我尝试搜索此问题,但无法找到解决方案,请帮助我:
我的viewModel:
namespace webShop.ViewModels.Home
{
public class RegisterUserViewModel
{
public User User { get; set; }
public UsersDetaile UsersDetaile { get; set; }
public IEnumerable<state> state { get; set; }
}
}
我的模特:
public partial class User
{
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 User User { get; set; }
}
和我的控制员:
[HttpGet]
public ActionResult Register()
{
OstanRepository stateRep = new OstanRepository();
var model = new RegisterUserViewModel();
model.state = stateRep.Select().ToList();
return View(model);
}
[HttpPost]
public ActionResult Register(RegisterUserViewModel user)
{
//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>
}
按提交用户且 usersDetaile null 后,我该怎么办?
当然,如果我使用这个动作,那就麻烦了!!!
public ActionResult Register(User user)
{
//...
}
我无法找到解决方案,请帮助我
答案 0 :(得分:1)
我看错了,你必须将ViewModel传递给View,目前你只是将ViewModel的User
属性传递给你的视图,这意味着你的视图被强类型化为User
输入RegisterUserViewModel
,这就是将操作参数更改为User
时的原因:
[HttpGet]
public ActionResult Register()
{
OstanRepository stateRep = new OstanRepository();
var model = new RegisterUserViewModel();
model.state = stateRep.Select().ToList();
return View(model); // pass ViewModel to view not only User Model
}
并且您应该使用RegisterUserViewModel
强烈输入您的视图:
将视图集模型设置为RegisterUserViewModel
@model webShop.ViewModels.Home.RegisterUserViewModel
答案 1 :(得分:1)
绑定失败,因为POST方法参数名为user
,而您的模型(RegisterUserViewModel
)包含具有相同名称的属性(public User User { get; set; }
)
将方法签名更改为
[HttpPost]
public ActionResult Register(RegisterUserViewModel model)
并且属性将被正确绑定。或者,您可以更改属性名称,但这意味着更新您的视图代码
`public User Person { get; set; }`