0

I am using MVC 2 and following the example here: Using Data Annotation Validators with the Entity Framework

When I click the Create button in my Create View nothing happens. The ModelState.IsValid is true always. What could be wrong?

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;
    }
}
}
Picflight
  • 3,832
  • 13
  • 61
  • 90
  • If I change the signature of my ActionResult as such: public ActionResult Create([Bind(Exclude = "ProductId")]Product collection) the collection object does not get populated with what I input in the form. – Picflight Mar 02 '10 at 07:02
  • If I change my View to a strongly-typed to Product instead of ProductFormViewModel, the validation works. Why wouldn't it work with ProductFormViewModel? – Picflight Mar 02 '10 at 16:19
  • If I change the signature of the ActionResult Create to public ActionResult Create([Bind(Exclude = "ProductId")]ProductFormViewModel model) I get an error "No parameterless constructor defined for this object." – Picflight Mar 02 '10 at 16:47

1 Answers1

0

I notice that you have the [Required] attribute on the Description property, but it is of type object. It looks like it should be a string. You are also passing a FormCollection to your Create action instead of your ViewModel.

It looks like your View is strongly-typed to a ProductFormViewModel which has a Product property. Change your Create Action:

public ActionResult Create([Bind(Exclude="ProductID")] ProductFormViewModel model)
Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • When I right click on the Create ActionResult in the controller, it creates the View with the FormCollection, why does it do that? Changing to ProductFormViewModel or Product as Charles suggests does not work either. – Picflight Mar 02 '10 at 05:30
  • Tried the Description property with string and object, nothing works. I am surprised that the the Membership forms, LogOn and Register work when I create a new MVC 2 project. – Picflight Mar 02 '10 at 05:38
  • have a look at my edit code in this question. i use a custom viewmodel and a strongly typed view: http://stackoverflow.com/questions/2347337/database-abstraction-layer-design-using-irepository-the-right-way – Alastair Pitts Mar 02 '10 at 05:40