如何使用Entity Framework验证MVC 2中的Form

时间:2010-03-02 01:51:46

标签: asp.net-mvc validation

我正在使用MVC 2并遵循此处的示例:Using Data Annotation Validators with the Entity Framework

当我在创建视图中单击“创建”按钮时,没有任何反应。 ModelState.IsValid始终为true。 可能出现什么问题?

Product.cs

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; }
}
}

ProductController.cs

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();
    }

Create.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  
Inherits="System.Web.Mvc.ViewPage<MyProject.Mvc.Models.ProductFormViewModel>" %>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>

     <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Product.ProductId) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Product.ProductId) %>
            <%= Html.ValidationMessageFor(model => model.Product.ProductId) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Product.ProductName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Product.ProductName) %>
            <%= Html.ValidationMessageFor(model => model.Product.ProductName) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.Product.Description) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Product.Description) %>
            <%= Html.ValidationMessageFor(model => model.Product.Description) %>
        </div>            
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

ProductFormViewModel.cs

using System.Web.Mvc;

namespace MyProject.Mvc.Models
{
public class ProductFormViewModel
{
    public Product Product { get; private set; }

    public ProductFormViewModel(Product product)
    {
        Product = product;
    }
}
}

1 个答案:

答案 0 :(得分:0)

我注意到Description属性上有[Required]属性,但它的类型为object。它看起来应该是一个字符串。您还将FormCollection传递给Create操作而不是ViewModel。

对于具有Product属性的ProductFormViewModel,您的View看起来是强类型的。更改您的创建操作:

public ActionResult Create([Bind(Exclude="ProductID")] ProductFormViewModel model)