Ajax.ActionLink publish the whole model in terms of?

I have a view that is strongly typed in a ViewModel. Is it possible to transfer all the data from the model in the view, back to the controller action? Something like that?

@Ajax.ActionLink("(Export to Excel)", "ExportCsv", "SurveyResponse", new { ResultsViewModel = Model }, new AjaxOptions {HttpMethod = "POST"}) 

And then collect data from ResultsViewModel as a parameter in another controller

 public ActionResult ExportCsv(ResultsViewModel resultsviewmodel) { } 
+6
source share
4 answers

No, you cannot pass an entire view model like this in an action reference. You can only pass the id this model, and then retrieve the actual model using this id , no matter where you originally retrieved it:

 @Ajax.ActionLink( "(Export to Excel)", "ExportCsv", "SurveyResponse", new { id = Model.Id }, new AjaxOptions { HttpMethod = "POST" } ) 

Alternatively, you can serialize the model as a javascript literal and then send it as JSON data with an AJAX request:

 @Html.ActionLink( "(Export to Excel)", "ExportCsv", "SurveyResponse", null, new { @class = "exportCsv" } ) <script type="text/javascript"> $('.exportCsv').click(function() { var model = @Html.Raw(Json.Encode(Model)); $.ajax({ url: this.href, type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(model), success: function(result) { } }); return false; }); </script> 
+4
source

Managed to do below work,

 @Ajax.ActionLink("(Export to Excel)", "ExportCsv", "SurveyResponse", new { Model.F1, Model.F2, Model.OtherFields }, new AjaxOptions {HttpMethod = "POST"}) 

controller

 [HttpPost] public ActionResult ExportCsv(ResultsViewModel resultsviewmodel) { } 

This is an http message, but the data is not in the "form data", it is encoded in the request URL (but not http-get).

It looks like MVC automatically converts individual fields into one model.

URL has length restrictions, a large model may fail.

+1
source

Try sending the model identifier to the controller and get the json result:

 @Ajax.ActionLink("(Export to Excel)", "ExportCsv", "SurveyResponse", new { id = Model.Id }, new AjaxOptions {HttpMethod = "POST"}) 

And in the controller you will have:

 [HttpGet] public ActionResult ExportCsv(int id) { //Here get the whole model from your repository for example: var model=GetModelByModelId(id); //And do everything you want with your model. return Json(model,JsonRequestBehavior.AllowGet); } 
0
source

I did the following. This somehow hits the HTTP POST point in my oppionion. But hey, he does his job.

My Ajax:

 @Ajax.ActionLink("Delete", "RemoveSubjectFromCategory","Categories", new { SubjectId = item.Id, CategoryId = Model.Id }, new AjaxOptions {HttpMethod = "GET"}) 

My controller:

 [HttpGet] public async Task<ActionResult> RemoveSubjectFromCategory(RemoveSubjectFromCategoryModel model) {} 

My binding model:

 public class RemoveSubjectFromCategoryModel { [Required] public int SubjectId { get; set; } [Required] public int CategoryId { get; set; } } 
0
source

Source: https://habr.com/ru/post/927395/


All Articles