回发到控制器的ViewModel为null

时间:2014-12-29 18:08:12

标签: asp.net-mvc asp.net-mvc-4 model-view-controller asp.net-mvc-5

我尝试搜索此问题,但无法找到解决方案,请帮助我

我的viewModel:

namespace webShop.ViewModels.Home
{
    public class RegisterUserViewModel
    {
        public UserDB User { get; set; }

        public UsersDetaile UsersDetaile { get; set; }

        public IEnumerable<state> state { get; set; }
    }
}

我的模特:

public partial class UserDB
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Roles { get; set; }
    public byte Status { get; set; }

    public virtual UsersDetaile UsersDetaile { get; set; }
}

public partial class UsersDetaile
{
    public int userID { get; set; }
    public Nullable<System.DateTime> BirthDate { get; set; }
    public string Mobile { get; set; }
    public string Tell { get; set; }
    public bool Gender { get; set; }
    public byte city { get; set; }
    public byte state { get; set; }
    public string postCode { get; set; }
    public string address { get; set; }

    public virtual City City1 { get; set; }
    public virtual State State{ get; set; }
    public virtual UserDB UserDB { get; set; }
}

和我的控制员:

[HttpGet]
public ActionResult Register()
{ 
    stateRepository stateRep = new stateRepository();
    var model = new RegisterUserViewModel();
    model.state = stateRep.Select().ToList();
    return View(model);
}

[HttpPost]
public ActionResult Register(RegisterUserViewModel user) //Problem: postback viewModel is null
{
   //My Code
}

和我的注册视图

@model webShop.ViewModels.Home.RegisterUserViewModel

@using (Ajax.BeginForm("Register", "Home", new AjaxOptions { HttpMethod = "Post", Url = "/Home/Register" }))
{

  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true)
  <h1 class="block-header">Register</h1>
      <div class="group">
          <label class="label">Name <span class="required">*</span></label>
          <div class="controls">
              @Html.TextBoxFor(p => p.User.Name, new { @class = "text" })
              @Html.ValidationMessageFor(p => p.User.Name)
          </div>
      </div>

    //....

  <div class="group">
        <label class="label">Address </label>
        <div class="controls">
            @Html.TextBoxFor(p => p.UsersDetaile.address, new { @class = "text" })
            @Html.ValidationMessageFor(p => p.UsersDetaile.address)
        </div>
  </div>

  <div class="form">
    <div class="group">
        <div class="controls">
             <button class="button">Submit</button>
        </div>
    </div>
  </div>
}

按提交 userDB usersDetaile null 后,我该怎么办?

2 个答案:

答案 0 :(得分:7)

如果您拥有复杂类型的ViewModels ... ,您需要调用复杂属性的默认构造函数

public class RegisterUserViewModel
{

    public RegisterUserViewModel() {
        User = new UserDB();
        UsersDetaile = new UsersDetaile();
    }

    public UserDB User { get; set; }

    public UsersDetaile UsersDetaile { get; set; }

    public IEnumerable<state> state { get; set; }
}

这样,绑定器可以加载属性值。你不需要任何其他东西。

至少看起来很奇怪的一件事是,你的ViewModel有一个名为UsersDetaile的属性,它与该类型的名称相同。同一属性位于UserDB内。这可能会使您和活页夹容易出错。

答案 1 :(得分:1)

要使用templated helpers绑定嵌套的ViewModel,它们与局部视图类似。所以要使你的代码工作:

  • EditorTemplates文件夹
  • 中创建新文件夹Home/View
  • 添加2个新文件UsersDetaile.cshtmlUserDB.cshtml
  • 将与类相关的标记移动到这些文件
  • 在您的主视图中改为:@Html.EditorFor(m => m.User)@Html.EditorFor(m => m.UsersDetaile)