views:

305

answers:

1

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

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