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?
For anyone else who looks at this you can do a @Html.EditorForModel() in a hidden div. You'd also have to use @Html.EditorFor(model => model.ObjectProperty) for each object property of the model.
This is already built in. Consider this model:
and now consider this action:
MVC will leverage the
FormCollection
and fill in theMyModel
class where it can. If you don't have thePropertyA
in the form then it will benull
. But since you have aninput
for theparameter
property it will be filled in.The entire model will be posted if you are using a FORM element. Your elements using the Model obviously need to be inside the form element
You can also POST the form yourself say by using JQuery
See this other stack issue for that : jQuery AJAX submit form
Have a close look at the anwser by "Alfrekjv"
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. SorryOne 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
You can check only the properties you want:
instead of: