MVC4复合型模式是后后空 [英] MVC4 Complex Type Model is null after post

查看:151
本文介绍了MVC4复合型模式是后后空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型

public class AdministrationModel
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string EmailAddress { get; set; }
  public bool IsApproved { get; set; }
}

这是我的控制器

public ActionResult GetTabContent(string id)
{
  switch (id)
  {
   case "tab3":
   model = GetAllUsersInfo();
   viewName = "Administration";
   break;
   }
   return View(viewName);
 }

  private List<AdministrationModel> GetAllUsersInfo()
  {
    List<AdministrationModel> userList = new List<AdministrationModel>();
    foreach (MembershipUser user in Membership.GetAllUsers())
    {
      UserProfile userProfile = UserProfile.GetUserProfile(user.UserName);
      userList.Add(new AdministrationModel { EmailAddress = user.Email,                       IsApproved = user.IsApproved, FirstName = userProfile.FirstName, LastName = userProfile.LastName });
    }

    return userList;
  }

这是我的看法。

@model List<AdminContainerModel>
@using (Html.BeginForm("Administration", "Account"))
{
  <fieldset>
    <div>
      @foreach (AdministrationModel AM in Model)
      {
        <div>
         <div class="colFull">@Html.DisplayFor(modelItem => AM.FirstName)</div>
         <div class="colFull">@Html.DisplayFor(modelItem => AM.LastName)</div>
         <div class="colFull">@Html.DisplayFor(modelItem => AM.EmailAddress)</div>
         <div class="colPartial"><input type="checkbox" checked="@AM.IsApproved"/>            </div>
      <div class="clear"></div>
    </div>
  }
</div>
 <input type="submit" value="Update Account" />
 </fieldset>
}

当用户点击更新帐户按钮它关系到控制器

When the user clicks the Update Account button it goes to the controller

  [HttpPost]
  public ActionResult Administration(List<AdministrationModel> model)
  {
     return View();
  }

这个方法中,模式始终为空。然而,呈现一切都是完美的,并显示我想让它显示什么视图。我在做什么错了?

inside this method, model is always null. however the View that renders everything is perfect and shows what I want it to show. What am I doing wrong?

推荐答案

在使用集合,以便正确地处理它们,所以它们是模型绑定上后没有任何额外的腿部的工作,你需要确保他们正确索引,您可以通过使用一个循环做到这一点,是这样的:

When using collections, in order to correctly process them so they are model-bound on post without any additional leg work, you need to make sure they are indexed correctly, you can do this by using a for loop, something like:

@for (int i = 0; i < Model.Count; i++)
{
    @Html.HiddenFor(m => m[i].FirstName)
    @Html.HiddenFor(m => m[i].LastName)
    @Html.HiddenFor(m => m[i].EmailAddress)
    <div>
        <div class="colFull">@Html.DisplayFor(m => m[i].FirstName)</div>
        <div class="colFull">@Html.DisplayFor(m => m[i].LastName)</div>
        <div class="colFull">@Html.DisplayFor(m => m[i].EmailAddress)</div>
        <div class="colPartial">@Html.CheckBoxFor(m => m[i].IsApproved)</div>
        <div class="clear"></div>
    </div>
}

这应该没有任何其他code型绑定:)

That should model bind without any other code :)

编辑:对不起我忘了,默认情况下displayFors不要把对模型绑定正确的属性,增加hiddenFors对于不具有editorFor其他领域

EDIT2:根据在评论你的另一个问题是,如果是面向公众的,你不希望他们更改任何隐藏的使用开发工具价值,请尝试以下操作:

好了,所以你不希望他们改变hiddenFors,这很好,但你会需要某种形式的ID,所以你知道哪些客户是当数据发布,我建议,而不是在上面有这些code:

Ok so you don't want them to change the hiddenFors, that's fine, but you will need some sort of ID so you know which client is which when the data is posted, I suggest that instead of having these in the above code:

@Html.HiddenFor(m => m[i].FirstName)
@Html.HiddenFor(m => m[i].LastName)
@Html.HiddenFor(m => m[i].EmailAddress)

替换为它们:

@Html.HiddenFor(m => m[i].ClientId)

你不回发的名字,姓氏或电子邮件地址,只是为了被选中,取消选中实际的客户端的引用的方式。

That way you're not posting back the firstname, lastname or email address, just a reference to the actual client that is ticked, unticked.

为了回答您的其他问题在关于跟踪原始值的,在你的控制器方法,你可以随便去从数据库中获取的原始值,那么这里是你如何能发现哪些是不同的评论,是这样的:

[HttpPost]
public ActionResult Administration(List<AdministrationModel> model)
{
    var originalMatches = GetAllUsersInfo();

    var differences = (from o in originalMatches
                      join c in model on o.ClientId equals c.ClientId
                      where c.IsApproved != o.IsApproved).ToList()

    return View();
}

这篇关于MVC4复合型模式是后后空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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