Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Model To Controller using Jquery/Ajax

I am trying to pass my model to a controller using JQuery/Ajax, I'm not sure how to do this correctly. So far I have tried using Url.Action but the model is blank.

Note: none of the duplicate threads on stackoverflow seem to address using ASP.NET 5 MVC 6.

View:

$("#inpDateCompleted").change(function () {         var url = '@(Url.Action("IndexPartial", "DashBoard", Model, null))';         $("#DailyInvoiceItems").load(url); }); 

Controller:

 [HttpGet]  public PartialViewResult IndexPartial(DashBoardViewModel m)  {       // Do stuff with my model       return PartialView("_IndexPartial");  } 
like image 464
Reafidy Avatar asked Nov 26 '15 22:11

Reafidy


People also ask

How to pass object from ajax to controller?

Show activity on this post. $. ajax({ type: "POST", url:"Home/Position", data: JSON. stringify({ array: positionarray }), cache: false, dataType: "json", contentType: "application/json; charset=utf-8", success: function (json) { } [HTTPPost] public void Position(YourClass[] array){...

How to pass object in jQuery ajax?

companyId = 531; $. ajax({ type: "POST", url: "TelephoneNumbers. aspx/DeleteNumber", data: "{numberId:1,companyId:531}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert('In Ajax'); } });

How pass JSON data to ajax to controller?

ajax({ type: "POST", url: "DATACRUD. json", data: JSON. stringify({data:"test"}), contentType: "application/json; charset=utf-8", dataType: "json", async: false, //_async, success: function (result) { } }); Ajax successfully invokes the action in a controller, but the parameter is null.


1 Answers

Looks like your IndexPartial action method has an argument which is a complex object. If you are passing a a lot of data (complex object), It might be a good idea to convert your action method to a HttpPost action method and use jQuery post to post data to that. GET has limitation on the query string value.

[HttpPost] public PartialViewResult IndexPartial(DashboardViewModel m) {    //May be you want to pass the posted model to the parial view?    return PartialView("_IndexPartial"); } 

Your script should be

var url = "@Url.Action("IndexPartial","YourControllerName")";  var model = { Name :"Shyju", Location:"Detroit"};  $.post(url, model, function(res){    //res contains the markup returned by the partial view    //You probably want to set that to some Div.    $("#SomeDivToShowTheResult").html(res); }); 

Assuming Name and Location are properties of your DashboardViewModel class and SomeDivToShowTheResult is the id of a div in your page where you want to load the content coming from the partialview.

Sending complex objects?

You can build more complex object in js if you want. Model binding will work as long as your structure matches with the viewmodel class

var model = { Name :"Shyju",                Location:"Detroit",                Interests : ["Code","Coffee","Stackoverflow"]             };  $.ajax({     type: "POST",     data: JSON.stringify(model),     url: url,     contentType: "application/json" }).done(function (res) {     $("#SomeDivToShowTheResult").html(res); }); 

For the above js model to be transformed to your method parameter, Your View Model should be like this.

public class DashboardViewModel {   public string Name {set;get;}   public string Location {set;get;}   public List<string> Interests {set;get;} } 

And in your action method, specify [FromBody]

[HttpPost] public PartialViewResult IndexPartial([FromBody] DashboardViewModel m) {     return PartialView("_IndexPartial",m); } 
like image 122
Shyju Avatar answered Oct 15 '22 15:10

Shyju