我要在“提交”表单上从“获取方法”中发送未使用的“模型”字段,如何将IEnumerable类型的字段发送回“帖子”?
我尝试使用
@Html.HiddenFor(model => model.MultiUserList)
@Html.HiddenFor(model => model.VerticalList)
但是我遇到了错误:
System.MissingMethodException: No parameterless constructor defined for this object.
这是MVC应用程序的所有部分
模型
public MultiSelectList MultiUserList { get; set; }
public IEnumerable<SelectListItem> VerticalList { get; set; }
我正在使用单独的类向我发送ViewModel
public ProjectViewModel GetProject(int id)
{
using (PMEntities2 db = new PMEntities2())
{
ProjectViewModel model = new ProjectViewModel();
//more code to populate other fields
model.VerticalList = GetVerticalList();
model.MultiUserList = GetMultiUserList();
return model;
}
}
GetMultiUserList()
public MultiSelectList GetMultiUserList()
{
PMEntities2 db = new PMEntities2();
List<tbUser> tbUserList = db.tbUsers.ToList();
IEnumerable<SelectListItem> UserList = tbUserList.Select(x => new SelectListItem()
{
Value = x.UserId.ToString(),
Text = x.UserName.ToString()
}).ToList();
MultiSelectList teamList = new MultiSelectList(UserList, "Value", "Text");
return teamList;
}
GetVerticalList()
public IEnumerable<SelectListItem> GetVerticalList()
{
PMEntities2 db = new PMEntities2();
List<tbVertical> tbVerticalList = db.tbVerticals.ToList();
IEnumerable<SelectListItem> VerticalList = tbVerticalList.Select(z => new SelectListItem()
{
Value = z.VerticalId.ToString(),
Text = z.VerticalName.ToString()
}).ToList();
return VerticalList;
}
控制器
[AuthorizeRoles("A")]
public ActionResult AddProject(int id = 0)
{
ProjectManager PM = new ProjectManager();
ProjectViewModel PVM = new ProjectViewModel();
PVM = PM.GetProject(id);
return View(PVM);
}
[AuthorizeRoles("A")]
[HttpPost]
public ActionResult AddProject(ProjectViewModel PVM)
{
if (ModelState.IsValid)
{
ProjectManager PM = new ProjectManager();
PVM.ProjectLeaderId = Convert.ToInt16(PVM.ProjectLeader);
int projectId = PM.AddProject(PVM);
}
}
AddProject()
public int AddProject(ProjectViewModel PVM)
{
PMEntities2 db = new PMEntities2();
tbProject project = new tbProject();
project.ProjectName = PVM.ProjectName.ToUpper();
project.ProjectVerticalId = PVM.VerticalId;
project.ProjectDescription = PVM.ProjectDescription;
if(PVM.StartDate == null)
{
project.ProjectStartDate = DateTime.Now;
}
project.ProjectStartDate = PVM.StartDate;
project.ProjectEndDate = PVM.EndDate;
project.ProjectLeader = PVM.ProjectLeaderId;
project.ProjectStatus = "ASSIGNED";
if (isProjectNameAlreadyExists(PVM) == true)
{
return 0;
}
db.tbProjects.Add(project);
db.SaveChanges();
return (project.ProjectId);
}
查看
@model PManager.Models.VIewModel.ProjectViewModel
@{
ViewBag.Title = "AddProject";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Add Project</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProjectName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectName,"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectDescription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectDescription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectDescription,"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectLeader, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.ProjectLeader, Model.TeamMembers , "--Leader--", new { id = "LeaderDropDown" })
@Html.ValidationMessageFor(model => model.ProjectLeader, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.VerticalId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.VerticalId, Model.VerticalList, "--Vertical--", new { id = "VerticalDropDown" })
@Html.ValidationMessageFor(model => model.VerticalId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.ProjectStatus , Model.StatusList, "--Status--", new { id = "StatusDropDown" })
@Html.ValidationMessageFor(model => model.ProjectStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SelectedMembers, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(x => x.SelectedMembers ,Model.MultiUserList, new { id = "MemberDropDown" })
@Html.ValidationMessageFor(model => model.SelectedMembers, "", new { @class = "text-danger" })
</div>
</div>
@Html.HiddenFor(model => model.MultiUserList)
@Html.HiddenFor(model => model.StatusList)
@Html.HiddenFor(model => model.VerticalList)
@Html.HiddenFor(model => model.TeamMembers)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script>
$(function () {
$("#MemberDropDown").chosen({
width: "50%",
placeholder_text_single: "Select Members",
no_results_text: "Oops, nothing found!"
});
$("#LeaderDropDown").chosen({
width: "50%",
placeholder_text_single: "Select Project Leader",
no_results_text: "Oops, nothing found!"
});
});
$.validator.setDefaults({
ignore: []
});
</script>