Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your View I don't see any text box or a field allowing to send data to the controller, only a label. Properties will <a href="http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html" rel="nofollow noreferrer">not be validated if they are not posted</a>. Add a textbox, leave it blank and your model won't be valid any more:</p> <pre><code>&lt;%= Html.TextBoxFor(m =&gt; m.Customer.Name.Title)%&gt; </code></pre> <hr> <p>UPDATE:</p> <p>Here's the code I've used:</p> <p>Model:</p> <pre><code>public class Name { [StringLength(10), Required] public virtual string Title { get; set; } } public class Customer { public virtual Name Name { get; set; } } public class CustomerViewModel { public Customer Customer { get; set; } } </code></pre> <p>Controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index([Bind(Prefix = "Customer")]Customer cs) { return View(new CustomerViewModel { Customer = cs }); } } </code></pre> <p>View:</p> <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MyApp.Models.CustomerViewModel&gt;" %&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;% using(Html.BeginForm()) { %&gt; &lt;%= Html.LabelFor(m =&gt; m.Customer.Name.Title)%&gt; &lt;%= Html.TextBoxFor(m =&gt; m.Customer.Name.Title)%&gt; &lt;button type="submit"&gt;Submit&lt;/button&gt; &lt;% } %&gt; &lt;/asp:Content&gt; </code></pre> <p>When you submit this form a validation error is shown.</p> <p>Remark1: I've omitted the <code>Entity</code> base class in the models as I don't how does it look.</p> <p>Remark2: I've renamed the variable in the Index action to <code>cs</code>. I remember that there was some problems with this in ASP.NET MVC 1.0 when you had the prefix and the variable named the same but I am not sure whether this applies here and I think it was fixed.</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