傳遞一個對象與HttpPost時有問題... 一旦提交表單,模型在控制器端設置爲「null」,我不知道在哪裏問題...net mvc4 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>
}
非常感謝您的幫助
您可以向控制器添加斷點並觀察Request.Form變量中的內容嗎?請展示它。 –
也顯示'User'和'Group'類 – GolfWolf
的代碼是'addusertemplate'對象爲空,或者只是它的屬性? – GolfWolf