使用 HttpPost 传递对象时出现问题...提交表单后,模型在控制器端设置为“null”,我不知道问题出在哪里。
这是我的控制器:
public ActionResult AddUser(int id = 0)
{
Group group = db.Groups.Find(id);
List<User> finalList = db.Users.ToList() ;
return View(new AddUserTemplate()
{
group = group,
users = finalList
});
//Everything is fine here, the object is greatly submitted to the view
}
[HttpPost]
public ActionResult AddUser(AddUserTemplate addusertemplate)
{
//Everytime we get in, "addusertemplate" is NULL
if (ModelState.IsValid)
{
//the model is null
}
return View(addusertemplate);
}
这是 AddUserTemplate.cs :
public class AddUserTemplate
{
public Group group { get; set; }
public User selectedUser { get; set; }
public ICollection<User> users { get; set; }
}
这是向控制器返回空值的表单(请注意,下拉列表中大量填充了好的值):
@using (Html.BeginForm()) {
<fieldset>
<legend>Add an user</legend>
@Html.HiddenFor(model => model.group)
@Html.HiddenFor(model => model.users)
<div class="editor-field">
//Here, we select an user from Model.users list
@Html.DropDownListFor(model => model.selectedUser, new SelectList(Model.users))
</div>
<p>
<input type="submit" value="Add" />
</p>
</fieldset>
}
非常感谢你的帮助