I'm new to ASP.NET MVC but I haven't been able to find an explanation for this.
My questions is regarding the difference in the value attribute in the generated HTML when I use @HtmlTextBox() vs. @HtmlTextBoxFor().
I can set the initial value for an <input> using @Html.TextBox() like this:
@Html.TextBox("Phone", "Initial Value", htmlAttributes: new { id = "txt_phone" })
The generated HTML is just what you'd expect:
<input id="txt_phone" name="Phone" type="text" value="Initial Value" />
Please notice the generated value attribute above.
Using @HtmlTextBoxFor() is a different. Please note that I have a very simple model in my view. It has a Phone property which is a String.
Here's an attempt at setting an initial value using @Html.TextBoxFor():
@Html.TextBoxFor(x => x.Phone, htmlAttributes: new { id = "txt_phone", value="Initial Value" })
The generated HTML, however, does not reflect the value attribute:
<input id="txt_phone" name="Phone" type="text" value="" />
My first question is, "why did the generated HTML not reflect the 'Initial Value' text in my value attribute?"
As many of you know, the "right way" to set the initial value with @HtmlTextBoxFor() is like this:
@Html.TextBoxFor(x => x.Phone, htmlAttributes: new { id = "txt_phone", Value = "Initial Value" })
But look at the generated HTML:
<input Value="Initial Value" id="txt_phone" name="Phone" type="text" value="" />
As you can see it generates a Value attribute (with a capital V) yet it still generates a value attribute with a lowercase v and an empty string!.
My second question then, is, why does @Html.TextBoxFor() require a captial V in Value yet still generate a lower-case v, value with an empty string?
Thanks