2012-05-09 53 views
1

我對我的視圖有問題,對於HTTP POST,視圖模型對於我的所有屬性返回nullMVC3的POST模型屬性爲空

以下是我的觀點模型。

public class CustomerVM 
{ 
    public List<CustomerCDTO> customerCDTO { get; set; } 
} 

在上述視圖模型我已經創建了一個List<CustomerCDTO>屬性。 CustomerCDTO類的定義如下。

public class CustomerCDTO 
{ 
    public string Name { get; set; } 
    public bool Active { get; set; } 
    public bool? IsChecked { get; set; } 
} 

下面是我的看法:

<%foreach (var item in Model.customerCDTO) {%> 
<tr> 
    <td style="text-align: center; width: 10%;" class="table-content-middle"> 
     <%if (item.Active == true) 
     {%> 
      <%=Html.CheckBoxFor(m=>item.Active)%> 
     <%} 
     else 
     { %> 
      <%=Html.CheckBoxFor(m=>item.Active)%> 
     <%}%> 
    </td> 
    <td class="table-content-middle" align="center" style="width: 80%;"> 
     <%: item.Name%> 
    </td> 
</tr> 
<%} %> 

當我執行HTTP GET一切正常,但在POST我收到nullCustomerVM.customerCDTO

請建議我該怎麼做才能使它工作。

感謝,

回答

4

那是因爲你沒有得到每個CustomerCDTO含這是一個List組成部分的信息表達。

使用for循環,而不是:

<%for (var i = 0; i < Model.customerCDTO.Count; ++i) 

,並參考使用表達式的元素,如

<%=Html.CheckBoxFor(m => m.customerCDTO[i].Active)%> 

基本上你需要有表達m => ...決心財產你有興趣從開始m,而不是來自其他變量。

+0

謝謝jon其工作正常。 – aamankhaan

+0

大家好,它對我的​​<%= Html.CheckBoxFor(m => item.Active)%>工作正常,但它不給我爲null <%:item.Name%> ...請建議 – aamankhaan

+0

@aamankhaan :什麼是「物品」?答案中沒有這樣的變量。 – Jon