我有一个带有Entity 6框架的MVC5项目,我无法将正确的信息传递给帖子上的编辑操作,以便将其绑定到模型。我的viewModel的ModelState返回无效。我不确定我错过了什么。我猜它与我的编辑器模板的设置方式与模型的结构有关,但我需要一些帮助来搞清楚。我花了很多时间来改变一些事情,试图让它发挥作用,我仍然无法纠正它。
我的ViewModel:
namespace CommunityHealth.Models.ViewModels
{
public class ActivityViewModel
{
public virtual IList<JunctionTypeAction> junctionTypeActions{ get; set; }
public virtual IList<JunctionDepartmentAction> junctionDepartmentActions{ get; set; }
public virtual IList<JunctionPopulationAction> junctionPopulationActions { get; set; }
public virtual CommunityAction Action { get; set; }
}
}
社区行动模式:
public partial class CommunityAction
{
public CommunityAction()
{
this.JunctionPopulationActions = new HashSet<JunctionPopulationAction>();
this.JunctionDepartmentActions = new HashSet<JunctionDepartmentAction>();
this.JunctionTypeActions = new HashSet<JunctionTypeAction>();
}
public int ActionID { get; set; }
public System.DateTime StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public string BreifDescription { get; set; }
public Nullable<int> Duration { get; set; }
public int LocationID { get; set; }
public string SubLocation { get; set; }
public int ProgramID { get; set; }
public string Notes { get; set; }
public string AddedBy { get; set; }
public byte[] RecordVersion { get; set; }
public virtual Location Location { get; set; }
public virtual Program Program { get; set; }
public virtual ICollection<JunctionPopulationAction> JunctionPopulationActions { get; set; }
public virtual ICollection<JunctionDepartmentAction> JunctionDepartmentActions { get; set; }
public virtual ICollection<JunctionTypeAction> JunctionTypeActions { get; set; }
}
接线表的模型: JunctionDepartmentAction:
public partial class JunctionDepartmentAction
{
public int IndexID { get; set; }
public int DepartmentID { get; set; }
public int ActionID { get; set; }
public string SubDepartment { get; set; }
public int Individuals { get; set; }
public virtual CommunityAction CommunityAction { get; set; }
public virtual Department Department { get; set; }
}
JunctionPopulationAction:
public partial class JunctionPopulationAction
{
public int IndexID { get; set; }
public int PopulationID { get; set; }
public int ActionID { get; set; }
public bool isActive { get; set; }
public virtual CommunityAction CommunityAction { get; set; }
public virtual TargetPopulation TargetPopulation { get; set; }
}
JunctionTypeAction:
public partial class JunctionTypeAction
{
public int IndexID { get; set; }
public int TypeID { get; set; }
public int ActionID { get; set; }
public virtual ActivityType ActivityType { get; set; }
public virtual CommunityAction CommunityAction { get; set; }
}
事件控制器编辑操作方法:
// GET: /Event/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CommunityAction communityaction = await db.CommunityActions.FindAsync(id);
ActivityViewModel activityviewmodel = new ActivityViewModel();
activityviewmodel.Action = communityaction;
IList<JunctionTypeAction> junctiontypeactions = await db.JunctionTypeActions.Where(d => d.ActionID == communityaction.ActionID).ToListAsync();
IList<JunctionDepartmentAction> junctiondepartmentactions = await db.JunctionDepartmentActions.Where(d => d.ActionID == communityaction.ActionID).ToListAsync();
IList<JunctionPopulationAction> junctionpopulationactions = await db.JunctionPopulationActions.Where(d => d.ActionID == communityaction.ActionID).ToListAsync();
activityviewmodel.junctionTypeActions = junctiontypeactions.ToList();
activityviewmodel.junctionDepartmentActions = junctiondepartmentactions.ToList();
activityviewmodel.junctionPopulationActions = junctionpopulationactions.ToList();
if (communityaction == null)
{
return HttpNotFound();
}
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", activityviewmodel.Action.LocationID);
ViewBag.ProgramID = new SelectList(db.Programs, "ProgramID", "ProgramID", activityviewmodel.Action.ProgramID);
return View(activityviewmodel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(ActivityViewModel activity)
{
//request added for debugging purposes
Request.ToString();
if (ModelState.IsValid)
{
db.Entry(activity).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", activity.Action.LocationID);
ViewBag.ProgramID = new SelectList(db.Programs, "ProgramID", "ProgramID", activity.Action.ProgramID);
return View(activity);
}
最后我的观点。我在DataBase中使用三个编辑器模板作为联结表,为CommunityAction对象使用另一个编辑器模板。然后我查看了使用三个编辑器模板显示ViewModel部分的事件。
CommunityAction.cshtml:
@model CommunityHealth.Models.CommunityAction
<div class="form-horizontal">
<h4>CommunityAction</h4>
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ActionID)
@Html.HiddenFor(model => model.RecordVersion)
<div class="form-group">
@Html.LabelFor(model => model.StartDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartDate)
@Html.ValidationMessageFor(model => model.StartDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndDate)
@Html.ValidationMessageFor(model => model.EndDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BreifDescription, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BreifDescription)
@Html.ValidationMessageFor(model => model.BreifDescription)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Duration, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Duration)
@Html.ValidationMessageFor(model => model.Duration)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LocationID, "Location", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Location, ViewBag.LocationID as SelectList, new { htmlAttributes = new { @class = "control-form" } })
@Html.ValidationMessageFor(model => model.Location)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SubLocation, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SubLocation)
@Html.ValidationMessageFor(model => model.SubLocation)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProgramID, "Program", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.ProgramID, ViewBag.ProgramID as SelectList, new { htmlAttributes = new { @class = "control-form" } })
@Html.ValidationMessageFor(model => model.Program)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Notes, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Notes)
@Html.ValidationMessageFor(model => model.Notes)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AddedBy, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AddedBy)
@Html.ValidationMessageFor(model => model.AddedBy)
</div>
</div>
<div class="form-group">
<div class="col-md-10">
</div>
</div>
</div>
JunctionTypeAction.cshtml:
@model CommunityHealth.Models.JunctionTypeAction
<div class="type">
<fieldset>
@Html.HiddenFor(model => model.ActionID)
@Html.HiddenFor(model => model.IndexID)
@Html.EditorFor(model => model.TypeID, "TypeName", new { @class = "control-label col-md-2" })
</fieldset>
</div>
JunctionDepartmentAction.cshtml:
@model CommunityHealth.Models.JunctionDepartmentAction
@using CommunityHealth.Models
<div>
<table>
<tbody>
<tr>
<td>
@Html.HiddenFor(model => model.ActionID)
@Html.HiddenFor(model => model.IndexID)
@Html.EditorFor(model => model.DepartmentID, "DepartmentName", new { @class = "control-label col-md-2" })
</td>
<td>
@Html.EditorFor(model => model.SubDepartment, "SubDepartment", new { @class = "control-label col-md-2 " })
</td>
<td>
@Html.EditorFor(model => model.Individuals)
</td>
</tr>
</tbody>
</table>
</div>
JunctionPopulationAction.cshtml:
@model CommunityHealth.Models.JunctionPopulationAction
<div class="population">
<fieldset>
@Html.HiddenFor(model => model.ActionID)
@Html.HiddenFor(model => model.IndexID)
@Html.EditorFor(model => model.PopulationID, "PopulationName", new { @class = "control-label col-md-2" })
</fieldset>
</div>
视图\事件\ Edit.cshtml:
@model CommunityHealth.Models.ViewModels.ActivityViewModel
@using CommunityHealth.Models.ViewModels;
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm("Edit","Event",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
<div class="form-group">
@Html.EditorFor(model => model.Action)
</div>
<div class="form-group">
@Html.Label("Types")
<div class="col-md-10">
<fieldset>
@for (int x = 0; x < Model.junctionTypeActions.Count(); x++)
{
@Html.EditorFor(model => model.junctionTypeActions[x])
}
</fieldset>
</div>
</div>
<div class="form-group">
@Html.Label("Departments")
<div class="col-md-10">
@for (int x = 0; x < Model.junctionDepartmentActions.Count(); x++)
{
@Html.EditorFor(model => model.junctionDepartmentActions[x])
}
</div>
</div>
<div class="form-group">
@Html.Label("Target Populations")
<div class="col-md-10">
@for (int x = 0; x < Model.junctionDepartmentActions.Count(); x++)
{
@Html.EditorFor(model => model.junctionPopulationActions[x])
}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我在检查请求时收到此错误:
“从'System.String'类型到'CommunityHealth.Models.CommunityAction'类型的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。”
答案 0 :(得分:0)
出现该错误的原因是因为您的模型具有名为activity
的属性(typeof CommunityAction
),并且您的post方法的参数也被命名为activity
。将参数名称更改为模型中属性名称以外的任何名称
public async Task<ActionResult> Edit(ActivityViewModel model)
{
....
}
旁注
@Html.EditorFor(m => m.junctionTypeActions)
即可生成
集合的html(不在for
循环中)。 @Html.EditorFor()
足够智能识别集合并生成正确的集合
的HTML。