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

Empty values on grid update with RemoteSaveAdaptor

Why is my data empty after grid Update with RemoteSaveAdaptor?

Controller:

List<VMFirmaRacun> model = new List<VMFirmaRacun>();

            foreach (var item in dsRacuni)
            {
                VMFirmaRacun fr = new VMFirmaRacun();
                fr.RacunId = item.RacunId;
                fr.FirmaId = item.FirmaId;
                fr.BankaId = item.BankaId;
                fr.Iban = item.Iban;
                fr.IntBrojFirme = item.IntBrojFirme;
                fr.Aktivan = item.Aktivan;
                model.Add(fr);
            }

ViewBag.dsRacuni = model.ToArray();

View:
<ejs-grid id="Grid" locale="hr-HR" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">
                            <e-data-manager json="@ViewBag.dsRacuni" updateUrl="/Maticni/Firmas/NormalUpdate" adaptor="RemoteSaveAdaptor"></e-data-manager>
                            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
                            <e-grid-columns>
                                <e-grid-column field="RacunId" isPrimaryKey="true" headerText="RacunId" width="50"></e-grid-column>
                                <e-grid-column field="FirmaId" headerText="FirmaId" width="50"></e-grid-column>
                                <e-grid-column field="BankaId" headerText="Banka" foreignKeyValue="Naziv" dataSource="ViewBag.dsBanka" validationRules="@(new { required=true})" width="50"></e-grid-column>
                                <e-grid-column field="Iban" headerText="IBAN" validationRules="@(new { required=true})" width="50"></e-grid-column>
                                <e-grid-column field="IntBrojFirme" headerText="Int. br." width="50"></e-grid-column>
                                <e-grid-column field="Aktivan" editType="booleanedit" displayAsCheckBox="true" type="boolean" headerText="Aktivan" width="50"></e-grid-column>
                            </e-grid-columns>
                        </ejs-grid>




Thanks!
Bernard.

11 Replies

PS Pavithra Subramaniyam Syncfusion Team December 26, 2018 09:02 AM UTC

 
Thanks for contacting Syncfusion support. 
 
We have analyzed the screenshot and the codes you have shared with us. We could see that you have not handled CRUD model class. You need to define CRUDModel(as generic) like below code example. Please find the below code example and sample for your reference. 
 
 
        public ActionResult Update([FromBody]CRUDModel<OrdersDetails> value) 
        { 
            var ord = value.value; 
            ... 
            return Json(value.value); 
        } 
 
        public class CRUDModel<T> where T : class 
        { 
 
            public T value { get; set; } 
        } 
 
 
 
Please refer the screenshot below, 
 
 
 
Please get back to us if you need further assistance. 
 
Regards, 
Pavithra S. 



BJ Bernard Jurlina December 26, 2018 09:25 AM UTC

Excellent, thanks Pavithra.

I guess that would be also the answer for my latest post about empty update with urladaptor?

Regards,
Bernard.


BJ Bernard Jurlina December 26, 2018 03:08 PM UTC

Hello Pavithra, and thank you again for your answers. 

Update is now working fine, when I used CRUDModel definition.
But, why is my value null when I try to insert new record?

<ejs-grid id="Grid" locale="hr-HR" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">
                            <e-data-manager json="@ViewBag.dsRacuni" updateUrl="/Maticni/Firmas/NormalUpdate" insertUrl="/Maticni/Firmas/NormalInsert" removeUrl="/Maticni/Firmas/NormalDelete" adaptor="RemoteSaveAdaptor"></e-data-manager>
                            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
                            <e-grid-columns>
                                <e-grid-column field="RacunId" isIdentity="true" isPrimaryKey="true" headerText="RacunId" width="50"></e-grid-column>
                                <e-grid-column field="FirmaId" headerText="FirmaId" width="50"></e-grid-column>
                                <e-grid-column field="BankaId" headerText="Banka" foreignKeyValue="Naziv" dataSource="ViewBag.dsBanka" validationRules="@(new { required=true})" width="50"></e-grid-column>
                                <e-grid-column field="Iban" headerText="IBAN" validationRules="@(new { required=true})" width="50"></e-grid-column>
                                <e-grid-column field="IntBrojFirme" headerText="Int. br." width="50"></e-grid-column>
                                <e-grid-column field="Aktivan" editType="booleanedit" displayAsCheckBox="true" type="boolean" headerText="Aktivan" width="50"></e-grid-column>
                            </e-grid-columns>
                        </ejs-grid>





Thanks!

Regards,
Bernard.


BJ Bernard Jurlina December 29, 2018 08:53 AM UTC

My grid has a column that is primary key and it is identity field.
I added the code for generating random number for it like this

function gridBegin(args) {
    if (args.requestType == "save") {
        args.data.RacunId = Math.random(); // Add the random value for primary key column. 
    }
}

and when I post it to the controller, I see the generated number



But still the value in the controller is null



I tried to set isIdentity="true" the the RacunId column, but it's the same results.

Any help?
Thanks.

Bernard.


PS Pavithra Subramaniyam Syncfusion Team December 31, 2018 12:26 PM UTC

  
We have validated your query and we suspect that the cause of the issue might be the type mismatch while setting the primary key manually. We can see that the primary key value is Whole number but you are setting a decimal Random value so in server you are receiving the null value. So We suggest you to set a whole number for the primary key column in your application. We have prepared a simple sample with RemoteSaveAdaptor with Insert operation in EJ2 Grid. Please refer the following code example and sample for more information. 
 
[Code example] 
[index.cshtml]    
<ejs-grid id="Grid" toolbar="@(new List<string>() { "Add","Delete","Update", "Cancel" })" allowPaging="true" actionBegin="onActionBegin" actionComplete="onActionComplete"> 
        <e-data-manager  json ="@ViewBag.datasource" adaptor="RemoteSaveAdaptor" updateUrl="Home/Update" insertUrl="Home/Insert" ></e-data-manager> 
        <e-grid-editSettings allowDeleting="true" allowEditing="true" allowAdding="true"></e-grid-editSettings> 
        <e-grid-columns> 
            <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" validationRules="@(new { required=true})" textAlign="Right" width="120"></e-grid-column> 
             .   .   .  .  
        </e-grid-columns> 
    </ejs-grid> 
</div> 
 
 
[HomeController.cs] 
 
public ActionResult Insert([FromBody]CRUDModel<OrdersDetails> value) 
        {           
            OrdersDetails.GetAllRecords().Add(value.value);    
 
            return Json(value.value); 
      } 
 
 
 
 
Please refer the below screenshot: 
 
 
Please get back to us, if you need further assistance, 
 
Regards, 
Pavithra S. 



BJ Bernard Jurlina December 31, 2018 12:58 PM UTC

Hi Pavithra!

Thank you for your answer. So, do I need to assign some value to identity primary key before saving?

The same solution like this I'm using in my ej1 mvc project, and grid is saving my data correctly with that random decimal number.

You have maybe some example with identity primary key?

Thanks.
Regards, Bernard.


PS Pavithra Subramaniyam Syncfusion Team January 2, 2019 10:35 AM UTC

 
We have achieved your requirement “Need to assign a decimal number in primary key value” using ActionBegin event of Grid component. We suggest you to set the primary key column type in server side while defining the field property. Please refer below code example and sample for more information. 
 
[index.cshtml] 
 
<ejs-grid id="Grid" toolbar="@(new List<string>() { "Add","Delete","Update", "Cancel" })" allowPaging="true" actionBegin="onActionBegin" actionComplete="onActionComplete"> 
        <e-data-manager  json ="@ViewBag.datasource" adaptor="RemoteSaveAdaptor" updateUrl="Home/Update" insertUrl="Home/Insert" ></e-data-manager> 
        <e-grid-editSettings allowDeleting="true" allowEditing="true" allowAdding="true"></e-grid-editSettings> 
        <e-grid-columns> 
            <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120" isIdentity="true"></e-grid-column> 
            .  .  .  . 
           <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column> 
        </e-grid-columns> 
    </ejs-grid> 
</div> 
<script> 
    function onActionBegin(args){    
        if (args.requestType === 'save' && args.action ==='add') { 
            args.data['OrderID'] = Math.random(); // Add a random value to primary key field 
        } 
    } 
 
 
[HomeController.cs] 
 
   public class OrdersDetails 
    { 
        public static List<OrdersDetails> order = new List<OrdersDetails>(); 
        public OrdersDetails() 
            { 
 
            } 
            public OrdersDetails(decimal OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress) 
            { 
 
          .  .  .  .  
 
            public decimal? OrderID { get; set; } 
            public string CustomerID { get; set; } 
            public int? EmployeeID { get; set; } 
            .  .  . 
       } 
 
 
Note: While enabling the IsIdentity property in a primary key column, then it should be handled in client or server side while inserting records to Grid. If corresponding data source has isIdentity field then it will automatically handle the Is Identity value while adding the record.  
 
 
 
Please get back to us, if you need further assistance, 
 
Regards, 
Pavithra S. 
  
 



BJ Bernard Jurlina January 8, 2019 11:44 PM UTC

Hi Pavithra,

and thank you for the answer. But, how can I change property from my RacunId field to decimal if it is integer type and autoincrement identity in the sql server database?
Is there then some way to assing new values for the inserted rows in the grid on the client side other than decimal? Because I'm using Math.Random() function.

I tried to avoid all of this by saving all the rows from the grid one by one after my master model is saved, on submit button.

Maybe there is some other and easier way to do this, but all I'm doing is that I'm sending datasource from the grid back to the controller over post action and then I'm checking for the RacunId values smaller than 1 and convert them to negative integer values in a row (-1, -2, -3...). After that I check existing values in the database for all the rows, and if row exists, I update its values. If row don't exists, I create a new row from received values.

My controller, model and view files are in attachment.

I would be grateful for some comment on this.
Thanks!

Regards,
Bernard.

Attachment: _CreateEdit_4b234e15.zip


PS Pavithra Subramaniyam Syncfusion Team January 9, 2019 09:05 AM UTC

Hi Bernard, 
 
Thanks for you provided sample code as it is helpful to resolve your requirements and we have checked your query and achieved your requirement with your code. 
 
Query #1: how can I change property from my RacunId field to decimal if it is integer type and autoincrement identity in the sql server database? 
 
If we bound the Grid data source using SQL table, we can enable the Auto increment column while table designing time. Please refer to the following online forum for more information, 
 
 
Query #2: Is there then some way to assigning new values for the inserted rows in the grid on the client side other than decimal? Because I'm using Math.Random() function. 
 
Yes, We have achieved your query and we suggest to you use following code example to handle the whole number conversion from “Math.Random” method. 
 
Query #3: How can I avoid all of this by saving all the rows from the grid one by one after my master model is saved, on submit button. 
 
As per your requirement,  we have achieved your requirement by using the save action in ‘actionBegin’ event. It is generating the random value to primary field for new records alone not for existing records. Please refer to the below code example for better assistance.  
 
Query 4#:  I'm sending datasource from the grid back to the controller over post action and then I'm checking for the RacunId(primary key field) values smaller than 1 and convert them to negative integer values in a row (-1, -2, -3...) 
 
We have validated you query and you can achieve your requirement by using the below code example. Please refer the below code sample and sample for more information. 
 
<div> 
    <input id="btnSpremi" type="submit" value="Spremi podatke firme" onclick="spremiFirmu()" class="btn btn-success" /> 
    @Html.Hidden("dsFirmaRacuni") 
    <ejs-grid id="Grid" toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" allowPaging="true" actionBegin="onActionBegin"> 
        <e-data-manager json="@ViewBag.datasource" adaptor="RemoteSaveAdaptor" updateUrl="Home/Update" insertUrl="Home/Insert"></e-data-manager> 
        <e-grid-editSettings allowDeleting="true" allowEditing="true" allowAdding="true"></e-grid-editSettings> 
        <e-grid-columns> 
            <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120" isIdentity="true"></e-grid-column> 
               .   .   .   .  
        </e-grid-columns> 
    </ejs-grid> 
</div> 
<script> 
    function onActionBegin(args){    
        if (args.requestType === 'save') { 
            if (args.action === 'add') { 
                args.data['OrderID'] = getRandomInt(1, 100); // Set the Randow value for primary key field while add a new record 
            } 
            var val = JSON.stringify(args.data);             
            $("#dsFirmaRacuni").val(val); // Set the New record or edited records in hidden element  
        } 
    } 
 
    function getRandomInt(min, max) { 
        min = Math.ceil(min); 
        max = Math.floor(max); 
        return Math.floor(Math.random() * (max - min + 1)) + min; 
    } 
 
    function spremiFirmu() {          
        alert($("#dsFirmaRacuni").val()); // get the new records or changes of existing values while click the submit button 
    } 
 
 
Please get back to us, if you need further assistance. 
 
Regards, 
Pavithra S. 



BJ Bernard Jurlina January 10, 2019 08:37 PM UTC

Thanks Pavithra,

that was really helpful. All of you here on the forum are doing really good job by helping us.
Thanks once again.

Regards,
Bernard.


PS Pavithra Subramaniyam Syncfusion Team January 11, 2019 05:03 AM UTC

Hi Bernard, 
 
Thanks for your update. 
 
We are happy to hear that the provided information helps you. 
 
Please contact us if you need any further assistance. 
 
Regards, 
Pavithra S. 


Loader.
Live Chat Icon For mobile
Up arrow icon