The crazy property of MVC is losing its value. Html.HiddenFor error?

There is something really strange going on in my MVC application that drives me crazy. On my MVC page, after the user has been selected, he should be twice "displayed" in HTML. Once in shape

FrmNextStep('<Login>', ... (where Model.SelectedUser.Login is used) 

and once in the form

 <input id="SelectedLogin" name="SelectedLogin" value="<Login>" type="hidden"> (where "Model".SelectedLogin is used) 

but the second always remains empty. This is really strange because, despite the fact that the two calls do not match, the return value should be.

 if (Model.SelectedUser != null) { <span>Some value</span> <script type="scriptADResultComplete"> @{ var script = String.Format( @"FrmNextStep('{0}', '{1}', '{2}');" , Model.SelectedUser.Login.Replace("'", @"\'") , Model.SelectedUser.FirstName.Replace("'", @"\'") , Model.SelectedUser.LastName.Replace("'", @"\'") ); @Html.Raw(script); } </script> } <input type="hidden" name="hfAction" /> <input type="hidden" name="hfUserLogin" /> @Html.HiddenFor(m => m.CurrentPage, new { id = "hfCurrentPage" }) @Html.HiddenFor(m => m.SelectedLogin); private User selectedUser; public User SelectedUser { get { if (this.selectedUser == null) { this.selectedUser = this.AllUsers.FirstOrDefault(user => user.Selected) ?? User.DefaultUser; } if (this.selectedUser == User.DefaultUser) { return null; } return this.selectedUser; } set { this.AllUsers.ForEach(user => user.Selected = (user == value)); this.selectedUser = null; } } public string SelectedLogin { get { return (this.SelectedUser ?? User.DefaultUser).Login; } set { this.SelectedUser = this.AllUsers.FirstOrDefault(user => user.Login == value); } } 

And when I debug the code, this is the only call to Selected Login during the "rendering" phase and returns the correct login.

Is there an error with Html.HiddenFor?

+6
source share
2 answers

Well, this is actually the error / erroneous behavior of Html.HiddenFor. The idea of ​​this opportunity only occurred to me when I wrote my question.


I changed it to:

 <input type="hidden" value="@Html.AttributeEncode(Model.SelectedLogin)" id="SelectedLogin" name="SelectedLogin" /> 

and it works great.

EDIT:

There is another desktop.

Call ModelState.Clear (); in action postter.
I will use this option.

 ModelState.Clear(); 

+

 @Html.HiddenFor(m => m.SelectedLogin) 
+11
source

I also had this problem: when executing the .cshtml command twice when returning the page, and the second run has problems with objects of the null model.

Using @Serge's answer fixes HiddenFor , but I also had problems with parts that cannot be resolved this way.

It turns out that this is an intermittent problem when the Controller method that ViewBag the view sets the ViewBag properties together with returning the view + model separately through protected internal ViewResult View(string viewName, object model); .

The project I'm working on has all this space, and on other controllers / views it works fine. But for the one I supported, this null issue in HiddenFor happening.

In any case, I pulled all ViewBag settings from the controller and moved them to the NotMapped properties of the NotMapped object.

Once this has been done, calls to HiddenFor , etc. everyone began to behave again. So, don't mix your ViewBag and ViewModel users.

0
source

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


All Articles