开发者

ASP.NET MVC2 model binding problem

Why is my controller receiving an empty model in this case?

Using

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<X.Models.ProductModel>" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

        <h2>Product</h2>

        <% using (Html.BeginForm() {%>

            <%: Html.ValidationSummary(true) %>

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

                <br />

                <div class="editor-label">
                    Short Description
                </div>
                <div class="editor-field">
                    <%: Html.TextAreaFor(model => model.ShortDesc) %>
                    <%: Html.ValidationMessageFor(model => model.ShortDesc) %>
                </div>

                <br />

                <div class="editor-label">
                    Long Description
                </div>
                <div class="editor-field">
                    <%: Html.TextAreaFor(model => model.LongDesc) %>
                    <%: Html.ValidationMessageFor(model => model.LongDesc) %>
                </div>

                <p>
                    <input type="submit" value="Create" />
                </p>

        <% } %>

    </asp:Content>

and the following controller.

using System.Web.Mvc;
using X.Lib.Services;
using X.Models;

namespace X.Admin.Controllers
{
    public class ProductController : Controller
    {

        [HttpGet]
        public ActionResult ProductData()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ProductData(ProductModel NewProduct)
        {
            //Validate and save
            if(ModelState.IsValid)
            {
                //Save And do stuff.
                var ProductServ = new ProductService();
开发者_StackOverflow社区                ProductServ.AddProduct(NewProduct);
            }

            return View();
        }

    }
}

Model:

public class ProductModel
{
    public int ID;

    [Required(ErrorMessage = "Name is required")]
    public string Name;

    public string LongDesc;
    public string ShortDesc;
}


EDIT: you need to use properties not variables

[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }

public string LongDesc { get; set; }

public string ShortDesc { get; set; }

Also...

You are not passing the model back to the view.

return View(NewProduct);

I normally pass a blank model in to the GET action too

ProductModel NewProduct = new ProductModel();
return View(NewProduct);

This way if you wish to set any default values you can do so easily.

Code example in full I've also added try and catch blocks around the adding of the product and given example views you could be returning on success or fail:

[HttpGet]  
public ActionResult ProductData()  
{  
     ProductModel NewProduct = new ProductModel();
    return View(NewProduct);  
}  

[HttpPost]  
public ActionResult ProductData(ProductModel NewProduct)  
{  
     //Validate and save  
     if(!ModelState.IsValid)  
     {  

    // Return the model back to view
    return View(NewProduct);

     }  

    try{
        //Save And do stuff.  
              var ProductServ = new ProductService();  
              ProductServ.AddProduct(NewProduct); 

    }
    catch(Exception){
           return View("Fail");
    }

     return View("Success");  
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜