Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation firing in ASP.NET MVC
    text
    copied!<p>I am lost on this MVC project I am working on. I also read Brad Wilsons article. <a href="http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html" rel="nofollow noreferrer">http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html</a></p> <p>I have this:</p> <pre><code>public class Employee { [Required] public int ID { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } } </code></pre> <p>and these in a controller:</p> <pre><code>public ActionResult Edit(int id) { var emp = GetEmployee(); return View(emp); } [HttpPost] public ActionResult Edit(int id, Employee empBack) { var emp = GetEmployee(); if (TryUpdateModel(emp,new string[] { "LastName"})) { Response.Write("success"); } return View(emp); } public Employee GetEmployee() { return new Employee { FirstName = "Tom", LastName = "Jim", ID = 3 }; } </code></pre> <p>and my view has the following:</p> <pre><code>&lt;% using (Html.BeginForm()) {%&gt; &lt;%= Html.ValidationSummary() %&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;div class="editor-label"&gt; &lt;%= Html.LabelFor(model =&gt; model.FirstName) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%= Html.DisplayFor(model =&gt; model.FirstName) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%= Html.LabelFor(model =&gt; model.LastName) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%= Html.TextBoxOrLabelFor(model =&gt; model.LastName, true)%&gt; &lt;%= Html.ValidationMessageFor(model =&gt; model.LastName) %&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; </code></pre> <p>Note that the only field editable is the LastName. When I postback, I get back the original employee and try to update it with <em>only</em> the LastName property. But but I see on the page is the following error:</p> <p>•The FirstName field is required.</p> <p>This from what I understand, is because the TryUpdateModel failed. But why? I told it to update only the LastName property. </p> <p><strong>I am using MVC2 RTM</strong></p> <p>Thanks in advance. </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