ViewModel 中的空值

发布于 2024-10-03 19:01:25 字数 1555 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2024-10-10 19:01:25

您应该确保该值实际存在于 POST 请求中。您得到 null 的事实强烈表明情况并非如此。您可以使用 FireBug 来准确查看发布到服务器的值。

另外,据我所知,您的表单仅包含商店名称和状态文本字段,因此这些是您可以期望在 POST 操作中返回的唯一值:

<% 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>
<% } %>

您可能还需要包括其他值。您可以使用隐藏字段来发送您需要的值。

在您的控制器操作中,您尝试读取 storeVM.Store.Active 字段,因此包括
它以你的形式:

<%: Html.HiddenFieldFor(model => model.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:

<% 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>
<% } %>

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 include
it in your form:

<%: Html.HiddenFieldFor(model => model.Store.Active) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文