开发者

null value in ViewModel

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");
        }


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) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜