ASP.NET MVC2模型绑定问题

时间:2010-05-19 09:34:27

标签: asp.net asp.net-mvc

为什么我的控制器在这种情况下会收到一个空模型?

使用

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

和以下控制器。

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();
                ProductServ.AddProduct(NewProduct);
            }

            return View();
        }

    }
}

型号:

public class ProductModel
{
    public int ID;

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

    public string LongDesc;
    public string ShortDesc;
}

1 个答案:

答案 0 :(得分:4)

编辑:你需要使用属性而不是变量

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

public string LongDesc { get; set; }

public string ShortDesc { get; set; }

也...

您没有将模型传回视图。

return View(NewProduct);

我通常也会将空白模型传递给GET动作

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

这样,如果您想设置任何默认值,您可以轻松完成。

完整的代码示例我还在添加产品时添加了try和catch块,并给出了可能在成功或失败时返回的示例视图:

[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");  
}