Is there a way to use @ Html.HiddenFor for the full model?

Is there a way to use (or something similar)

@Html.HiddenFor 

for the whole model.

So, instead of doing something like this:

  @Html.HiddenFor(model => Model.Person.Name) @Html.HiddenFor(model => Model.Person.LastName) @Html.HiddenFor(model => Model.Address.Street) 

use something like this (this example does not work)

  @Html.HiddenFor(model => Model) 

I already searched for it in stackoverflow and google ... but found nothing about it.

I need to hold some values ​​for different models that are not stored in db, so using only Html.HiddenFor ID is not an option for me.

Thanks in advance!

+6
source share
2 answers

Select the properties you want to hide and add HiddenInputAttribute to your model as follows:

 public class MyModel { [HiddenInput] public int MyProperty { get; set; } } 
+7
source

You can take a look at the MVCContrib Html.Serialize method . This is sorta viewstate emulation (and inside it really uses ViewState :-)).

An alternative approach would be to simply save the unique identifier as a hidden field, and inside the controller action use this identifier to retrieve the appropriate model from your base data store.

+3
source

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


All Articles