视图模型上HttpPost返回null [英] viewmodel return null on HttpPost

查看:78
本文介绍了视图模型上HttpPost返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建像下面的景色。

 <%为(VAR I = 0; I< Model.customerCDTO.Count();我++)
  {%GT;
  &所述; TR>
     < TD风格=文本对齐:中心;宽度:10%;类=表内容中间的>
     <如果%(Model.customerCDTO [I]。主动== true)而
      {%GT;
      <%= Html.CheckBoxFor(M = GT; m.customerCDTO [I]。主动)%GT;
      <%}
      其他
      {%GT;
      <%= Html.CheckBoxFor(M = GT; m.customerCDTO [I]。主动)%GT;
    <%}%GT;
     < / TD>
     < TD类=表的内容,中间的ALIGN =中心的风格=宽度:80%;>
     <%= Html.DisplayTextFor(M = GT; m.customerCDTO [I] .Name点)%GT;
     < / TD>
     < / TR>
 <%}%GT;

与上述观点的问题是,它是用于空赋予值customerCDTO [I] .Name点

下面是我的视图模型

 公共类CustomerVM
{
公开名单< CustomerCDTO> customerCDTO {搞定;组; }
}

在上面的视图模型我创建了一个列表属性。该CustomerCDTO类的定义如下:

 公共类CustomerCDTO
{
公共字符串名称{;组; }
公共BOOL活跃{搞定;组; }
公共BOOL? {器isChecked获得;组; }
}

请帮忙。

感谢


解决方案

它给你一个价值,因为你的表单中不包含任何&LT ;输入>对应 元素名称属性(如果你查看​​源代码,你可以立即发现)。这样做的原因是, DisplayTextFor 是不是应该建立在第一时间这样的元素。

几乎总是坏主意将也称 Html.HiddenFor ,其中确实的创建输入元素:

 < TD类=表的内容,中间的ALIGN =中心的风格=宽度:80%;>
    <%= Html.DisplayFor(M = GT; m.customerCDTO [I] .Name点)%GT;
    <%= Html.HiddenFor(M = GT; m.customerCDTO [I] .Name点)%GT;
< / TD>

这将解决你的问题,但用户可以简单地用类似Firebug的工具编辑值并更改名称,你显然不,因为想要的价值否则,你就不得不presented一个&LT;输入&GT; 元素的。 (注:我也改变了 DisplayTextFor DisplayFor ,里面一般是做事情比较正确的做法)<。 / p>

但怎么办呢?好了,这里没有神奇的解决方案。如果表单不包含名称那么这将是,但如果它的确实包含名称(甚至是隐藏的),那么用户可以随意更改。如果这是一个问题,唯一的选择是修改后端code来填充名称从您的数据存储。

i have created a view like below.

<%for (var i = 0; i < Model.customerCDTO.Count();i++)
  {%>
  <tr>
     <td style="text-align: center; width: 10%;" class="table-content-middle">
     <%if (Model.customerCDTO[i].Active == true)
      {%>
      <%=Html.CheckBoxFor(m=>m.customerCDTO[i].Active)%>
      <%}
      else
      { %>
      <%=Html.CheckBoxFor(m => m.customerCDTO[i].Active)%>
    <%} %>
     </td>
     <td class="table-content-middle" align="center" style="width: 80%;">
     <%=Html.DisplayTextFor(m=>m.customerCDTO[i].Name)%>
     </td>
     </tr>
 <%} %>

the problem with the above view is that it is giving value for null for customerCDTO[i].Name

below is my view model

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

In the above view model I have created a List property. The CustomerCDTO class definition is as follows.

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

please help.

thanks

解决方案

It's giving you a null value because your form does not contain any <input> elements corresponding to the Name property (which you can immediately spot if you View Source). The reason for this is that DisplayTextFor is not supposed to create such elements in the first place.

An almost always bad idea would be to also call Html.HiddenFor, which does create input elements:

<td class="table-content-middle" align="center" style="width: 80%;">
    <%=Html.DisplayFor(m=>m.customerCDTO[i].Name)%>
    <%=Html.HiddenFor(m=>m.customerCDTO[i].Name)%>
</td>

This would solve your problem, but then the user could simply edit the values with a tool like Firebug and change the value of Name, which you obviously do not want since otherwise you 'd have presented an <input> element for that. (Note: I also changed DisplayTextFor to DisplayFor, which in general is the more correct way to do things).

But what to do then? Well, there's no magic solution here. If the form doesn't contain Name then it will be null, but if it does contain Name (even hidden) then the user can change it at will. If that's a problem, the only option is to modify your backend code to populate Name from your data store.

这篇关于视图模型上HttpPost返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆