在 MVC 4 视图中,我在提交期间将值分配给隐藏。但是我没有将该值返回给控制器。这是我的代码
@model Web.Models.FeaturedContactViewModel
@using (Html.BeginForm("Update", "User", FormMethod.Post))
{
@Html.HiddenFor(m => m.FeaturedSelected);
<input type="submit" value="Update" onclick="return onSubmit()" />
}
@section scripts
{
<script type="text/javascript">
function onSubmit() {
var selectedOptions = $.map($('#FeaturedContacts option'),
function (e) { return $(e).val(); });
$("#FeaturedSelected").val(selectedOptions.join(","));
return true;
};
</script>
}
控制器
[HttpPost]
public ActionResult FeaturedContacts(FeaturedContactViewModel model)
{
return View();
}
FeaturedContactViewModel
public class FeaturedContactViewModel
{
public List<Contact> AllContacts { get; set; }
public List<Contact> FeaturedContacts { get; set; }
public int[] AllSelected { get; set; }
public int[] FeaturedSelected { get; set; }
}