Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an entire model on form submission

Tags:

I understand that I can use @Html.HiddenFor(m => m.parameter) and when the form is submitted, that parameter will be passed to the controller. My model has many properties.

Is there a shorter way of passing the entire model at once to the controller or must I do it one by one each time?

like image 509
jpo Avatar asked Mar 20 '13 15:03

jpo


People also ask

How do you pass data into a model?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How do I pass multiple models from view to controller?

Use a query parameter to denote which form is being posted. Use a different form name, but still with the view model. Use a redirect-only Action for the form (send new instances, and only part of the viewmodel) Use a mixture of the above.

How many ways are there to submit a form in asp net MVC?

There are a total of 13 overloaded ways to use and implement @Html. BeginForm.


1 Answers

The model will be passed to the controller in its entirety, but the values of properties that are not bound by input or hidden fields will be lost.

You have to either bind the properties in the form on the client-side, or re-fetch the entity on the server-side.

You seem to be asking for something like @Html.HiddenFor(m => m.Model), and that is not possible. Sorry

One thing to keep in mind, if you have tons of hidden fields, you may be sending more data to the view than you really need. Consider employing view models

like image 184
Forty-Two Avatar answered Sep 29 '22 22:09

Forty-Two