2014-01-17 83 views
3

對不起,這裏有大量的代碼,但它是解釋發生的最好方法。MVC 4 Ajax.BeginForm POST不綁定到控制器中的模型

我在MVC 4局部視圖給這個代碼:

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 
    @using (Ajax.BeginForm("TestPost", new AjaxOptions { HttpMethod = "Post" })) 
    {        
     foreach (var d in Model.DataItemsWithLabels) 
     {          
      @Html.LabelFor(m => d.DataName) 
      @Html.TextBoxFor(m => d.DataValue);    
     } 

     <input type="submit" value="Save" /> 
    } 

我的控制器操作如下:

public ActionResult TestPost(CmaPartialModel model) 
{   
     return PartialView("Transaction", model); 
} 

在我的模型我有一些代碼,填充對象的列表(只有當它們是空的時)定義爲:

public List<DataItemWithLabel> DataItemsWithLabels { get; set; }  

    public class DataItemWithLabel 
    { 
     public string DisplayName { get; set; } 

     public string DataName { get; set; } 

     [Required] 
     public string DataValue { get; set; } 
    } 

我也有什麼想法是正確的web.config中的條目:

我已閱讀這裏沒有成功別處職位的負載。

該代碼發佈到TestPost方法,但模型始終爲空。有人可以告訴我爲什麼這可能是?

回答

6

您總是得到null值的原因是模型綁定器不知道如何綁定對象列表。見下面的例子:

public class CmaPartialModel 
{ 
    public List<DataItemWithLabel> DataItemsWithLabels { get; set; } 
} 

然後用它在您查看:

@model CmaPartialModel 

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 
@using (Ajax.BeginForm("TestPost", new AjaxOptions { HttpMethod = "Post" })) 
{ 
    for(var i = 0; i < Model.DataItemsWithLabels.Count; i++) 
    { 
     @Html.LabelFor(m => m.DataItemsWithLabels[i].DataName) 
     @Html.TextBoxFor(m => m.DataItemsWithLabels[i].DataValue) 
    } 
    <input type="submit" value="Save" /> 
} 

最後,您的操作方法應該是象下面這樣:

public ActionResult TestPost(CmaPartialModel model) 
{   
     return PartialView("Transaction", model); 
} 

更新

你也可以在您的視圖中使用foreach,如下所示:

foreach (var item in Model.DataItemsWithLabels.Select((value, i) => new { i, value })) 
{ 
    @Html.LabelFor(m => m.DataItemsWithLabels[@item.i].DataName) 
    @Html.TextBoxFor(m => m.DataItemsWithLabels[@item.i].DataValue); 
} 
+0

感謝林 - 我已經有一個列表完全像我的ViewModel - 我稱之爲CmaPartialModel。你的解決方案几乎可以工作,但我可以看到的唯一區別是for循環而不是foreach - 爲什麼foreach是問題或者我錯過了什麼? – davy

+0

hi @davy,問題不在於使用for或foreach來循環所有的DataItemWithLabel模型,你可以使用任何一個。問題是當你使用foreach循環所有的項目時,model binder不知道如何將數據綁定到它。因此,您需要創建類似索引的內容來標記所有項目,然後模型綁定器綁定所有項目列表的數據。我更新了關於如何使用foreach來做到這一點的答案,請看看它。 – Lin

0

您需要修改安置自己的方法如下

public ActionResult TestPost(IEnumerable<DataItemWithLabel> model) 
{   
     return PartialView("Transaction", model); 
} 

既然你是通過你的DataItemWithLabel車型集合到控制器。

+0

謝謝 - 這很有道理 - 儘管我的DataItemWithLabel在模型CmaPartialModel內,因此返回null。 – davy

1
I think below code will help you. 

查看

@model MvcApplication1.Models.CmaPartialModel 
@{ 
ViewBag.Title = "Index"; 
} 
<h2>Index</h2> 
@using (Ajax.BeginForm("TestPost", new AjaxOptions { HttpMethod = "Post" })) 
{ 
    if (Model != null) 
    { 
    foreach (var d in Model.DataItemsWithLabels) 
    {          
     @Html.LabelFor(m => d.DataName) 
     @Html.TextBoxFor(m => d.DataValue); 
    } 
    } 
    <input type="submit" value="Save" /> 
} 

型號

public class CmaPartialModel 
{ 
    public List<DataItemWithLabel> DataItemsWithLabels { get; set; } 

    public class DataItemWithLabel 
    { 
     public string DisplayName { get; set; } 

     public string DataName { get; set; } 

     public string DataValue { get; set; } 
    } 
} 

控制器

public ActionResult Index() 
    { 
     return View(); 
    } 
    public ActionResult TestPost(CmaPartialModel model) 
    { 
     List<CmaPartialModel.DataItemWithLabel> parts = new  List<CmaPartialModel.DataItemWithLabel>(); 
     parts.Add(new CmaPartialModel.DataItemWithLabel() {DisplayName = "abc",DataName = "cbv",DataValue = "343"}); 
     parts.Add(new CmaPartialModel.DataItemWithLabel() { DisplayName = "sfda", DataName = "xzfdsf", DataValue = "234" }); 
     model.DataItemsWithLabels = parts; 
     return PartialView("Index", model); 
    }