Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate Form in MVC 2 using Entity Framework
    text
    copied!<p>I am using MVC 2 and following the example here: <a href="http://www.asp.net/learn/mvc/tutorial-39-cs.aspx" rel="nofollow noreferrer">Using Data Annotation Validators with the Entity Framework</a></p> <p>When I click the Create button in my Create View nothing happens. The ModelState.IsValid is true always. <strong>What could be wrong?</strong></p> <p><strong>Product.cs</strong></p> <pre><code>using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace MyProject.Mvc.Models { [MetadataType(typeof(ProductMetaData))] public partial class Product { } public class ProductMetaData { [Required(ErrorMessage = "Description is required")] public object Description { get; set; } } } </code></pre> <p><strong>ProductController.cs</strong></p> <pre><code>public ActionResult Create() { Product portal = new Product() { }; return View(new ProductFormViewModel(portal)); } [HttpPost] public ActionResult Create([Bind(Exclude = "ProductId")]FormCollection collection) { if (ModelState.IsValid) { try { // TODO: Add insert logic here return RedirectToAction("Index"); } catch { } } // If we got this far, something failed, redisplay form return View(); } </code></pre> <p><strong>Create.aspx</strong></p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;MyProject.Mvc.Models.ProductFormViewModel&gt;" %&gt; &lt;%= Html.ValidationSummary("Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;div class="editor-label"&gt; &lt;%= Html.LabelFor(model =&gt; model.Product.ProductId) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%= Html.TextBoxFor(model =&gt; model.Product.ProductId) %&gt; &lt;%= Html.ValidationMessageFor(model =&gt; model.Product.ProductId) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%= Html.LabelFor(model =&gt; model.Product.ProductName) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%= Html.TextBoxFor(model =&gt; model.Product.ProductName) %&gt; &lt;%= Html.ValidationMessageFor(model =&gt; model.Product.ProductName) %&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%= Html.LabelFor(model =&gt; model.Product.Description) %&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%= Html.TextBoxFor(model =&gt; model.Product.Description) %&gt; &lt;%= Html.ValidationMessageFor(model =&gt; model.Product.Description) %&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; </code></pre> <p><strong>ProductFormViewModel.cs</strong></p> <pre><code>using System.Web.Mvc; namespace MyProject.Mvc.Models { public class ProductFormViewModel { public Product Product { get; private set; } public ProductFormViewModel(Product product) { Product = product; } } } </code></pre>
 

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