asp.net mvc - How do you send data to controller with ajax.beginform? -


this method on controller "sale"

[httppost]         [validateantiforgerytoken]         public actionresult create(models.account account)         {             models.sale creaventa = new models.sale();             //creaventa.account = cliente;             creaventa.createdon = datetime.now;             creaventa.idaccount = account.id;             creaventa.modifiedon = datetime.now;             creaventa.status = 0;              context.sales.add(creaventa);             context.savechanges();             // return "venta creada";             return view();         } 

and partial view

@model list<modal3.models.account>  @{     viewbag.title = "create"; } <select class="form-control" id="control1">      @{          foreach (var cliente in model)         {             <option value="@cliente.id"> @cliente.name</option>          }    } </select>  @*@using (html.beginform("create", "sale", formmethod.post, new {id="my-form" })) {     @html.antiforgerytoken()     <button type="submit" class="btn btn-default" value="create" id="btncrear">         iniciar venta     </button>  }*@  @using (     ajax.beginform("create","sale",new ajaxoptions()     {         httpmethod ="post",         insertionmode = insertionmode.replace,       })     ) {     @html.antiforgerytoken()     <button type="submit" class="btn btn-default" value="create" id="btncrear">         iniciar venta     </button>  } 

this enter method not send model.

then:

  • how send model?
  • how send lot of objects?

this model

//------------------------------------------------------------------------------ // <auto-generated> //    code generated template. // //    manual changes file may cause unexpected behavior in application. //    manual changes file overwritten if code regenerated. // </auto-generated> //------------------------------------------------------------------------------  namespace modal3.models {     using system;     using system.collections.generic;      public partial class sale     {         public sale()         {             this.saledetails = new hashset<saledetail>();         }          public int id { get; set; }         public nullable<system.datetime> createdon { get; set; }         public nullable<system.datetime> modifiedon { get; set; }         public nullable<int> status { get; set; }         public nullable<int> idaccount { get; set; }          public virtual account account { get; set; }         public virtual icollection<saledetail> saledetails { get; set; }     } }       using system;     using system.collections.generic;     using system.linq;     using system.web;     using system.componentmodel.dataannotations;     using system.web.mvc;      namespace modal3.models     {          [metadatatype (typeof (sale_validation ))]         public partial class sale         {          }          public  class sale_validation         {              //2015-06-17 22:07:26.353   2015-06-17 22:07:26.353 1   1             [display (name="")]             [hiddeninput (displayvalue =false )]             public nullable<system.datetime> createdon { get; set; }             [display(name = "")]             [hiddeninput(displayvalue = false)]             public nullable<system.datetime> modifiedon { get; set; }             [display(name = "")]             [hiddeninput(displayvalue = false)]             public nullable<int> status { get; set; }             public nullable<int> idaccount { get; set; }          }     } 

to make understand how ajax form works, created below code -

lets our model -

public class sale {     public string saleowner { get; set; }         public virtual account account { get; set; } }  public class account {     public string name { get; set; } } 

i created two controller actions -

 public actionresult adatas()  {      return view();  }   [httppost]  public jsonresult create(sale s)  {       return json("true");  } 

the first controller action return following view -

@model webapplication1.controllers.sale  <script src="~/scripts/jquery-1.10.2.min.js"></script> <script src="~/scripts/jquery.unobtrusive-ajax.min.js"></script>  @using (ajax.beginform("create", "sale", new ajaxoptions() {     insertionmode = insertionmode.replace,     updatetargetid = "done" })) {     @html.textboxfor(m => m.saleowner)     @html.textboxfor(m => m.account.name)      <input type="submit" value="click" /> }  <div id="done"> </div> 

view renders follows -

enter image description here

once click on button, breakpoints in code -

enter image description here

once ajax post happens, output -

enter image description here


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -