We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Foreign Key Column - Boolean Value

Hello,

i've an Entity with an Boolean property "Status". This Field iam displaying in my Grid. If i change this Field, at Serverside nothing is changing. The Value (true/false) is not getting tranfered correctly.

public partial class Mitarbeiter
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Mitarbeiter()
        {
        }
    
        public int Id { get; set; }
        public string Personalnummer { get; set; }
        public string Vorname { get; set; }
        public string Nachname { get; set; }
        public string Email { get; set; }
        public bool Status { get; set; }
--------------------------------------------------------------------------

I've bound this Colum to my Grid with Foreign Key-Settings:

 @(Html.EJ().Grid<object>("MitarbeiterGrid")
      .Datasource(ds=> ds.Json((IEnumerable<object>)ViewBag.datasource2).CrudURL("Mitarbeiter/CrudUpdate").Adaptor(AdaptorType.RemoteSaveAdaptor)).AllowPaging()
      .EditSettings(edit => { edit.AllowAdding().AllowEditing().EditMode(EditMode.Normal); })
      .ClientSideEvents(cse=>cse.EndEdit("alertOK"))
      .Locale("de-DE")
      .AllowSorting()
      .IsResponsive(true)
      .EnableResponsiveRow(true)
            .ToolbarSettings(toolbar =>
            {
                toolbar.ShowToolbar().ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems.Add);
                    items.AddTool(ToolBarItems.Edit);
                    items.AddTool(ToolBarItems.Delete);
                    items.AddTool(ToolBarItems.Update);
                    items.AddTool(ToolBarItems.Cancel);
                    items.AddTool(ToolBarItems.Search);
                });
            })
      .Columns(col =>
      {
          col.Field("Id").Visible(false).IsPrimaryKey(true).Add();
          col.Field("Personalnummer").Add();
          col.Field("Vorname").Add();
          col.Field("Nachname").Add();
          col.Field("Email").Add();
          col.Field("Status").ForeignKeyField("wert").ForeignKeyValue("anzeige").DataSource((IEnumerable<object>)ViewBag.status).Add();
          col.Field("MitarbeiterTyp_Id").HeaderText("Mitarbeitertyp").ForeignKeyField("Id").ForeignKeyValue("Typname").DataSource((IEnumerable<object>)ViewBag.datasource).Add();
      })

---------------------------------------------------------------------------------
Controller:
public ActionResult Index()
        {
            ModelContext cont = new ModelContext();
            var data = cont.MitarbeiterTyp.ToList();
            var data2 = cont.Mitarbeiter.Include("MitarbeiterTyp").ToList();
          
            ViewBag.datasource = data;
            ViewBag.datasource2 = data2;
            var dat = new List<Status>() { new Status() {wert=true, anzeige="Aktiv"}, new Status() { wert =false, anzeige = "Inaktiv" } };
            ViewBag.status = dat.ToList();
            return View();
        }

public class Status
    {
        public bool wert;
        public string anzeige;
    }


public ActionResult CrudUpdate(Mitarbeiter value, string action) <---- Entity value.Status is all the Time at false
        {
            if (action== "update")
            {
                cont.Entry(value).State = EntityState.Modified;
                cont.SaveChanges();
            }
            else if (action == "insert")
            {
                Mitarbeiter neuerMA = value;
                cont.Mitarbeiter.Add(neuerMA);
                cont.SaveChanges();
            }
           ...
        }
    }


5 Replies

VA Venkatesh Ayothi Raman Syncfusion Team June 7, 2016 12:17 PM UTC

Hi Arthur, 

Thanks for contacting Syncfusion support. 

We have validated your provided code and mentioned issue, we have suspect that using “ForeignKey” columns in Grid. The “ForeignKeyField” must be placed in parent table. Please find the code example : 


[cshtml] 
  @(Html.EJ().Grid<object> 
                ("FlatGrid") 
                        . . . 
                    .Columns(col => 
                    { 
                    col.Field("Id").HeaderText("ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add(); 
                    col.Field("Email").HeaderText("Email").Width(80).Add(); 
                    col.Field("Nachname").HeaderText("Nachname").TextAlign(TextAlign.Right).Width(75).Add(); 
                    col.Field("Personalnummer").HeaderText("Personalnummer").TextAlign(TextAlign.Right).Width(75).Add(); 
                    col.Field("Status").HeaderText("Status").ForeignKeyField("Status").ForeignKeyValue("anzeige").DataSource((IEnumerable<object>)ViewBag.foreign).TextAlign(TextAlign.Right).Width(75).Add(); 
                    col.Field("Vorname").HeaderText("Vorname").TextAlign(TextAlign.Right).Width(75).Add(); 
 
                    })) 
 
 
[c#] 
 
  public ActionResult GridFeatures() 
        { 
            var DataSource = new NorthwindDataContext().OrdersViews.ToList(); 
            
            int id = 100; 
            for (int i = 1; i < 9; i++) 
            { 
                tab.Add(new Table1() { Id = id+1, Email = "dbakj@abc.com", Nachname = "bjask", Personalnummer = "bjsak", Status = true, Vorname = "dbaksj" }); 
. . . 
                id += 6; 
            } 
 
            List<Table2> table = new List<Table2>(); 
            //Status as foreignkey 
            table.Add(new Table2() { Status = false, anzeige = "chjay", FirstName = "cvusxh" }); 
. . . 
            ViewBag.datasource = tab; 
            return View(); 
        } 
 
 


Note:  The ForeignKeyField and Grid Column Field must be same.  the Foreign key is defined in a second table, but it refers to the primary key in the first table. 
 
 
Please find the screen shot: 
 
 


Please refer to the UG Documentation about “ForeignKey” column:  




Regards, 
Venkatesh Ayothiraman. 



AR Arthur June 7, 2016 01:13 PM UTC

Hello,

thanks for the hints, but everything mentioned from you, is allready ok in my code.
Now i have renamed the key "wert" to "Status" as in the parent table, but still the same issue.

The "Foreign Key Field" is in the Parent table! Like i privided it allready in the Code.
My Parent Table is the Entity "Mitarbeiter".

With other Foregin Key Column it is allready working fine and correctly, but only the Boolean-Column does doing problems.

Here is what is send after i changed the Status Column from "Inaktiv"(false) to "Aktiv"(true):





The send Request:




VA Venkatesh Ayothi Raman Syncfusion Team June 8, 2016 11:25 AM UTC

Hi Arthur, 

Thanks for the update. 

The cause of the issue is the ForeignkeyField which is not unique. If we set foreignkeyfield as Boolean column then it has values like true or false only, this is not a unique value. When we edit the Grid in which Boolean value is “true” then we can iterate the Foreignkey DataSource for which ForeignkeyField has “true” value. So, this case Foreignkey field has multiple true value which causes the issue. Please refer to the below screenshot for detailed explanation,   

Screenshot 1:  Foreignkey datasource: 
 
Screenshot 2: when we editing the Grid: 
 
Screenshot 3: After saving the record: 
 

So we have suggest you to set unique values for ForeignKeyField. 

Regards, 
Venkatesh Ayothiraman. 



AR Arthur June 8, 2016 12:39 PM UTC

THis is the Solution:

Ragavee U S [Syncfusion]
Updated on Jun 08, 2016 07:57 AM
Hi Arthur, 

Thanks for contacting Syncfusion support. 

We analyzed the reported query and we are able to reproduce the issue in the below scenario. 

Scenario: When localization is enabled in the sample and Boolean column is foreign key column such that upon editing and saving the column, returns value as “false” always. 

The above issue is obtained due to conflict that arises between the localized value of the Boolean values (true and false) that is set using the ej.Grid.Locale object and the value that is bound to the dataSource of the foreign key column. 

This can be handled in either of the following ways. 

1.       If you are not displaying the model class property directly in the grid instead displaying it using the foreign key concept, then we suggest to exclude the below definitions corresponding to the boolean column localization. 
 
<script>   
    ej.Grid.Locale["de-DE"] = { 
        True: "true", 
        False: "false" 
    } 
</script> 
 
2.       Else, if you want the localized values to be displayed in grid for any other boolean column(not the foreign key column) and thus have set the localized values for the Boolean values using ej.Grid.Locale, then we suggest you to use the below code example. 

<script>     
    //actionBegin event of the Grid 
    function onBegin(args) { 
        if (args.requestType == "save") { 
            var dropObj = $("#GridVerified").ejDropDownList("instance"); 
            args.data.Verified = dropObj.getSelectedValue();//set the column value based on dropdown selected value 
        } 
    } 
     
</script> 


Regards, 
Ragavee U S. 


VA Venkatesh Ayothi Raman Syncfusion Team June 9, 2016 12:10 PM UTC

Hi Arthur, 

Sorry for the Inconvenience caused. 

We are missed to analyze whether you are using Locale property in Grid which is in provided code. We are happy to hear that your requirement is achieved. 

Thanks, 
Venkatesh Ayothiraman.  


Loader.
Live Chat Icon For mobile
Up arrow icon