Note that there are some explanatory texts on larger screens.

plurals
  1. POTextBox(HtmlHelper, String) - is view model also searched for a value?
    text
    copied!<p>Pro Asp.Net MVC 4 (pg 535):</p> <blockquote> <p>The string argument is used to search the view data, ViewBag and view model to find a corresponding data item that can be used as the basic for the input element. So, for example, if you call <code>@Html.TextBox("DataValue")</code>, the MVC Framework tries to find some item of data that corresponds with the key DataValue. The following locations are checked: • ViewBag.DataValue • @Model.DataValue</p> </blockquote> <p>Book states that an overload of a Html.TextBox which takes a single string argument checks both <code>ViewData</code> and <em>view model</em> to obtain the value for display. But after experimenting a little, it seems it only checks <code>ViewData</code> for a value, but not also a <em>view model</em>.</p> <p>Thus, in the output generated from <code>Html.TextBox</code> the <code>value</code> attribute is set to an <em>empty string</em>:</p> <pre><code>public class Person { public string FirstName = "Leila"; } </code></pre> <p>*.cshtml:</p> <pre><code>@model HelperMethods.Models.Person ... @Html.TextBox("FirstName") </code></pre> <p>Output:</p> <pre><code> ... &lt;input id="FirstName" name="FirstName" type="text" value="" /&gt; </code></pre> <p>So is the book wrong and <code>TextBox(HtmlHelper, String)</code> checks only <code>ViewData</code> or does it also check a <em>view model</em>?</p> <p><strong>UPDATE:</strong></p> <pre><code>public class Person { public string FirstName = "Leila"; } public class HomeController : Controller { public ActionResult Index() { return View((object)new Person()); } } </code></pre> <p>Index.cshtml:</p> <pre><code>@model Mvc4.Controllers.Person @{ Layout = null; } &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="viewport" content="width=device-width" /&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; @Html.TextBox("FirstName") &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Much appreciated</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload