2011-02-01 87 views
0

我有一個問題,應該是一個非常簡單的ASP.NET MVC Create方法。HttpPost中創建ASP.NET MVC錯誤 - POST後的模型中傳遞的空值 - 模型綁定問題?

在填充數據並在創建視圖中提交表單(並且傳遞了所有驗證規則)之後,傳遞給POST方法的模型實例將爲每個字段包含null或0值,ModelState.IsValid將返回false。

我使用Firebug來檢查POST表單參數,並且提交操作顯然已經發布了來自這些字段的所有參數。但是,這些參數並未綁定到Controller中傳遞給POST方法的模型,因此,模型(即代碼中的材料)在所有字段中都包含空值(如使用Watch和置於POST開頭的斷點檢查方法)。從螢火蟲

POST參數:

Content-Type: application/x-www-form-urlencoded Content-Length: 228 MaterialNoMass.MaterialNoMassID=9mmPlasterboard&MaterialNoMass.RoughnessTypeID=3&MaterialNoMass.Resistance=0.0560&MaterialNoMass.ThermalAbsorptance=0.09&MaterialNoMass.SolarAbsorptance=0.07&MaterialNoMass.VisibleAbsorptance=0.07 

創建視圖:在控制器中創建的

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Database.Master" 
Inherits="System.Web.Mvc.ViewPage<MvcDeepaWebApp.ViewModels.DatabaseSimpleMaterialViewModel>" %> 
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 
     <% Html.EnableClientValidation(); %> 
     <% using (Html.BeginForm()) 
      {%> 
     <%: Html.ValidationSummary(true) %> 
     <fieldset> 
      <legend>Create MaterialNoMass</legend> 
      <%: Html.EditorFor(model => model.MaterialNoMass, new { RoughnessTypes = Model.RoughnessTypes })%> 
      <p> 
       <input type="submit" value="Save" /> 
      </p> 
     </fieldset> 
     <% } %> 
     <div> 
      <%: Html.ActionLink("Back to List", "Index") %> 
     </div> 
    </asp:Content> 

GET和POST方法:

// 
    // GET: /DatabaseSimpleMaterial/Create 
    public ActionResult Create() 
    { 
     var viewModel = new DatabaseSimpleMaterialViewModel 
     { 
      MaterialNoMass = new MaterialNoMass(), 
      RoughnessTypes = deepaDB.RoughnessTypes.ToList() 
     }; 

     return View(viewModel); 
    } 

    // 
    // POST: /DatabaseSimpleMaterial/Create 

    [HttpPost] 
    public ActionResult Create(MaterialNoMass material) 
    { 
     if (ModelState.IsValid) 
     { 
      MaterialAllType mt = new MaterialAllType(); 
      mt.MatID = material.MaterialNoMassID; 
      mt.MatTypeID = deepaDB.MaterialTypes.Single(type => type.MatType == "SimpleMaterial").MatTypeID; 
      material.MatTypeID = mt.MatTypeID; 

      deepaDB.AddToMaterialAllTypes(mt); 

      deepaDB.AddToMaterialNoMasses(material); 

      deepaDB.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     //Error occurred 
     var viewModel = new DatabaseSimpleMaterialViewModel 
     { 
      MaterialNoMass = material, 
      RoughnessTypes = deepaDB.RoughnessTypes.ToList() 
     }; 

     return View(viewModel); 
    } 

僅供參考,只要創建一個新的MaterialNoMass ,還應使用相同的MaterialNoMass ID創建MaterialAllType的新實例。我還沒有達到修改數據庫的階段,因爲模型綁定似乎不起作用。

我在應用程序中的其他控制器中使用了類似的創建方法,除了在此控制器中它們都工作正常。過去兩天我一直在調試這個問題。請幫忙!

回答

1

問題出在您傳遞給視圖的模型爲DatabaseSimpleMaterialViewModel並且在您期望的POST操作內部爲MaterialNoMass。所以一定要使用正確的前綴:

[HttpPost] 
public ActionResult Create([Bind(Prefix="MaterialNoMass")]MaterialNoMass material) 

我想你編輯模板生成這樣的領域:

<input type="text" name="MaterialNoMass.MaterialNoMassID" value="" /> 
<input type="text" name="MaterialNoMass.RoughnessTypeID" value="" /> 
... 

所以,你需要表明模型綁定是有這樣的前綴。

+0

謝謝Darin!有用!我仍然想知道爲什麼我可以避免在我的其他控制器中設置任何綁定前綴,這些控制器在GET中傳遞ViewModel(填充下拉列表),並且只使用模型類作爲POST操作的參數。 – fsalim 2011-02-01 13:59:59