问题描述
我试图寻找这个问题,但无法为它找到一个解决方案,请帮我
i try to search this problem, but could not find a solution for it, please help me:
我的视图模型:
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>
}
在preSS提交的用户和 usersDetaile 为 空的,我能做些什么?
after press submit user and usersDetaile is null, what can i do?
当然,如果我用这个acction,这是借机!
Of course, if i use this acction, it's worke!!!
public ActionResult Register(User user)
{
//...
}
我不能找到一个解决方案,请帮我
推荐答案
绑定失败( RegisterUserViewModel
)包含具有相同的名称(公众用户用户{获取;设置;}属性
)
The binding fails because you POST method parameter is named user
and your model (RegisterUserViewModel
) contains property with the same name (public User User { get; set; }
)
您的方法签名更改为
[HttpPost]
public ActionResult Register(RegisterUserViewModel model)
和属性会被正确绑定。或者,您可以更改属性名称,但这就意味着更新您的看法code
and the properties will be correctly bound. Alternatively, you can change the property name but this would mean updating your view code
`public User Person { get; set; }`
这篇关于回发视图模型为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!