TextBoxFor - display the wrong value

OK, I have a weird one here. I know that the value is passed to the property correctly, as this works fine:

@Html.TextBox("Foo", Model.Foo, new { @class = "bar" }) 

Now if I do this:

 @Html.TextBoxFor(m => m.Foo, new { @class = "bar" }) 

Displays the wrong value. I do not know where this value comes from. For some pages, it shows a line version of what it should be; at other times, it shows the value of the text box next to it. I am puzzled. This is the only text block that does this. I also have a razor helper on the page that uses this exact value to display the page title, and it displays correctly.

I am not opposed to using @Html.TextBox() for this particular one, but I would like to get the bottom of this.

Has anyone had something random like this? I have quite a few controls on this particular page, and this is the only thing that happens.

+6
source share
3 answers

Oh God ... I just found the culprit. This is the URL routing value! lol

In my global file, I had {something}/{whatever}/{id}/{foo} , {foo} , which is an optional parameter, just to make the page URL look friendly.

Interestingly, the expression used in TextBoxFor is pulled from the URL, not from the viewmodel. I would think that I would read the model before navigating to the url? Even intellisense draws from the model. It's some kind of mistake?

Note to yourself: Always make sure that the properties have different names!

Hope this experience helps others.

+9
source

Could you place some code from the view, where this happens, as well as the model? This is probably something unlike the @Html.TextBoxFor() method.

0
source

This happened to me when I made an ajax call to create an entity and, after success, reloaded the list of entities with a partial view. During the reload, the text fields in the view took on the value of the new object added for all elements in the list. Adding ModelState.Clear(); to server code fixed.

0
source

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


All Articles