2010-05-19 103 views
2

爲什麼我的控制器在這種情況下接收空模型?ASP.NET MVC2模型綁定問題

使用

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

回答

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

I'vre試過了! ModelState.IsValid始終返回true,然後嘗試保存並獲取錯誤,因爲該模型不包含輸入到文本框中的值。在模型的填充中存在着不對的現象。 – LiamB 2010-05-19 10:57:07

+0

你可以發佈模型。你有驗證嗎? – Andrew 2010-05-19 11:04:33

+0

其中一天。 – LiamB 2010-05-19 12:18:34