ViewModel 中的空值
我有一个 ViewModel (StoreViewModel)。当我从视图到控制器(Post)获取虚拟机中的值时,某些值为空,只有视图上显示的某些值不为空,
请帮助我
public class StoreViewModel
{
public StoreViewModel() { }
public Store Store { get; set; }
public List<Member> Members { get; set; }
public List<Order> Orders { get; set; }
public List<AccountPayable> AccountsPayable { get; set; }
}
这是我的视图,
<% using(Html.BeginForm()) {%>
<div><%: Html.TextBoxFor(model => model.Store.Name) %></div>
<div><%: Html.TextBoxFor(model => model.Store.State) %></div>
<div><input type="submit" value="Submit" /></div>
<% } %>
我在列上设置了 [HiddenInput (DisplayValue = false)]实体商店、会员、订单、AccountPayable
+++
这是我的控制器。 (我尝试使用 FormCollection 从 View 获取值,但是......不成功)
[HttpPost]
public ActionResult Details(Finger finger, StoreViewModel storeVM)
{
//if (finger.roleName != "Administrator")
// return RedirectToAction("DisplayNotice", "Notice");
storeVM.Store.Active = (CheckBoxHelpers.GetValue(storeVM.Store.Active)).ToString();
if (ModelState.IsValid)
{
storesRep.SaveStore(storeVM.Store, true);
}
else
{
return View(storeVM);
}
return RedirectToAction("List", "Stores");
}
I has a ViewModel (StoreViewModel). When I get value in VM from View to Controller (Post), some value is null, only some value that is shown on View is not null
Plz help me
public class StoreViewModel
{
public StoreViewModel() { }
public Store Store { get; set; }
public List<Member> Members { get; set; }
public List<Order> Orders { get; set; }
public List<AccountPayable> AccountsPayable { get; set; }
}
Here is my View
<% using(Html.BeginForm()) {%>
<div><%: Html.TextBoxFor(model => model.Store.Name) %></div>
<div><%: Html.TextBoxFor(model => model.Store.State) %></div>
<div><input type="submit" value="Submit" /></div>
<% } %>
I set [HiddenInput (DisplayValue = false)] on columns in Entity Store, Member, Order, AccountPayable
+++
Here is my Controller. (I tried use FormCollection to get value from View, but...unsuccess)
[HttpPost]
public ActionResult Details(Finger finger, StoreViewModel storeVM)
{
//if (finger.roleName != "Administrator")
// return RedirectToAction("DisplayNotice", "Notice");
storeVM.Store.Active = (CheckBoxHelpers.GetValue(storeVM.Store.Active)).ToString();
if (ModelState.IsValid)
{
storesRep.SaveStore(storeVM.Store, true);
}
else
{
return View(storeVM);
}
return RedirectToAction("List", "Stores");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该确保该值实际存在于 POST 请求中。您得到
null
的事实强烈表明情况并非如此。您可以使用 FireBug 来准确查看发布到服务器的值。另外,据我所知,您的表单仅包含商店名称和状态文本字段,因此这些是您可以期望在 POST 操作中返回的唯一值:
您可能还需要包括其他值。您可以使用隐藏字段来发送您需要的值。
在您的控制器操作中,您尝试读取
storeVM.Store.Active
字段,因此包括它以你的形式:
You should make sure that the value is actually present in the POST request. The fact that your are getting
null
is a strong indication that this is not the case. You could use FireBug to see exactly what values are posted to the server.Also from what I can see your form contains only store name and state text fields so those are the only values you can expect to get back in your POST action:
You might need to include the others as well. You could use hidden fields to send the values you need.
In your controller action you are trying to read the
storeVM.Store.Active
field so includeit in your form: