How can I pass the model in jquery as a JSON variable? -


on mvc/razor page, have view model 1 element list of model. list written view using partial view. if list has 3 elements, there 3 rows on view. want send value of entire list using json can carry out calculations. how can pass values of model or elements in list part of model controller using json?

so page looks this:

------------------------- |                       | | | id |  | price |     | | | id |  | price |     | | | id |  | price |     | |                       | ------------------------- 

i want send every id , price (a variable number of rows) controller using json perform calculations. this:

 $("#btncreatecontract").click(function () {      var url = '/createcontract/checksalesslips';      $.getjson(         url         ,          {            cashprice: $("#txtcashdownpayment_rev").val()            , salesslip: model          }         , function (response)         {         status = response.status;         }        );      return status; }); 

the controller has method this:

    [httpget]     public jsonresult checksalesslips(string cashprice, modelsalesslips salesslips)     {         decimal total = 0.00m;         foreach (var item in salesslips)         {             total = total + item.price;         }          if (total.tostring() != cashprice) // check sales slip item equals amount financed.         {             return json(new { status = true }, jsonrequestbehavior.allowget);         }         else         {             return json(new { status = false }, jsonrequestbehavior.allowget);         }      } // checksalesslips 

however, i'm getting message saying model not defined. tried

salesslip: '<%= model.salesslips %>' 

and

salesslip: '<%= model %>' 

and other variations, saleslip variable blank when gets controller.

if had 1 control (i.e. there 1 price field) use id of control value. in case, however, have variable number of controls.

can tell me how pass model through json in jquery? if isn't possible, how pass variable number of control values controller?

if you've seen previous posts, first mvc project , i'm new razor, jquery, , json well, please go easy on me. thanks!

here's other code:

the main model page, modelapplication.cs:

public class modelapplication {     public int id { get; set;      public list<modelsalesslip> salesslips { get; set; } } 

the model elements in list, modelsalesslip:

public class modelsalesslip {     public int id { get; set; }     public decimal price { get; set; } } 

relevant parts of view, createcontract.mobile.cshtml:

@model models.modelapplication @{     viewbag.title = "createcontract.mobile";     layout = "~/views/shared/_adminmaster.mobile.cshtml";     htmlhelper.clientvalidationenabled = true; } @using (html.beginform(new { id="formcreatecontract", @class = "form-horizontal ", @commandname = "modelapplication" })) {    @{      foreach (var item in model.salesslips)      {        <tr>       @{          html.renderpartial("createcontractpartial", item);        }       </tr>      } <input id="btncreatecontract" type="submit" value="save" name="createcontract" /> } 

the partial view, createcontractpartial.mobile.cshtml:

<div class="editorrow"> @using (html.begincollectionitem("salesslips"))  {     <td> @html.textboxfor(x => x.id, new { @class = "wide" })</td>     <td> @html.textboxfor(x => x.price, new { @class = "wide" })</td>  }  </div> 

you want return items yet post method has parameter modelsalesslips salesslips (i.e. 1 modelsalesslips item, not collection).

you can use .serialize() function send form control values controller

var url = '@url.action("checksalesslips", "createcontract")'; // don't hardcode urls! $("#btncreatecontract").click(function () {   $.getjson(url, $(form.serialize(), function(response) {     ....   }); }); 

and controller method be

public jsonresult checksalesslips(string cashprice, list<modelsalesslips> salesslips) 

you have not shown input id="txtcashdownpayment_rev", assuming has name="cashprice" , within <form> tags, value bound parameter cashprice in method (but should using view model additional property decimal cashprice { get; set; } , use type helper create input, post public jsonresult checksalesslips(yourviewmodel model)).

however, since seem comparing total of decimal value, parameter cashprice should type of decimal, not string.

however seems unnecessary waste of band width when perform check on client - like

var total = 0; $(.editorrow).each(function(index, item) {   total += new number($(this).find('input[type="text"]).last().val()); }); if (new number($("#txtcashdownpayment_rev").val()) == total) {   .... 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -