Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c# - TextBoxFor - Showing Wrong Value

Ok, I've got a weird one here. I know the value is being 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" })

It show's an incorrect value. I have absolutely no idea where this value is coming from. For some pages, it shows a lowercase version of what it should be, other times, it shows the value of the textbox next to it. I'm baffled. It's the only textbox that does this. I also have a razor helper on the page that uses this exact value to display the heading of the page, and that shows correctly.

I don't mind using @Html.TextBox() for this particular one, but I'd like to get the bottom of this.

Anyone else had anything random like this happen? I have quite a few controls on this particular page and it's the only one this happens to.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Oh jeez... I just found the culprit. It's the url routing value! lol

In my global file, I had {something}/{whatever}/{id}/{foo}, {foo} being an optional parameter there just to make the page url look human friendly.

It is interesting that the expression used in TextBoxFor is pulling from the URL rather than the viewmodel. I would have thought it'd read the model before going to the URL? Even intellisense pulls from the model. Is this some sort of a bug?

Note to self: Always make sure properties have different names!

Hopefully this experience will help others.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...