我在mvc4 + EF6项目中发帖时遇到问题。视图中的模型将填充除年份和期间以外的所有字段(它们是下拉列表字段)。在附加到视图的视图模型中,未将DataAnnotations设置为Year和Period字段。但是,当我尝试发布时,ModelState无效。
如何确定ModelState无效的原因?
见下面的代码:
HTML:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Status)
<div class="form-horizontal">
<fieldset>
<legend>Edit Performance Cycle Details</legend>
<div class="form-group">
@Html.LabelFor(model => model.Year, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
@Html.DropDownList("Year", new SelectList(ViewBag.Years), "--- Select Cycle Year ---", new { @class = "form-control", @id = "Year" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Period, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
@Html.DropDownList("Period", new SelectList(ViewBag.Period, "Id", "Title"), "--- Select Period ---", new { @class = "form-control", @id = "Period" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
@Html.EditorFor(model => model.StartDate, "EditorForCycleSD")
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CloseDate, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
@Html.EditorFor(model => model.CloseDate, "EditorForCycleCD")
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
@Html.ActionLink("Cancel", "Index", "Cycle", null, new { @class = "btn btn-default" })
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</fieldset>
</div>
}
控制器代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "StartDate, CloseDate, Id, Status")] EditCycle cycleUpdate, string Year, string Period)
{
try
{
if (ModelState.IsValid)
{
int yearPost = Int32.Parse(Year);
string periodPost = Period.Trim();
switch (periodPost)
{
case "1":
periodPost = yearPost.ToString() + " - H1";
break;
case "2":
periodPost = yearPost.ToString() + " - H2";
break;
default:
return View(cycleUpdate);
}
int pf = Int32.Parse(User.Identity.Name.Substring(8));
cycleUpdate.Year = yearPost;
cycleUpdate.Period = periodPost;
if (this.repo.EditCycle(cycleUpdate, pf))
{
return View("Index");
}
else
{
List<CyclePeriod> period = new List<CyclePeriod>{
new CyclePeriod {Id=1, Title="First Half : January 1 - June 30"},
new CyclePeriod {Id=2, Title="Second Half : July 1 - December 31"}
};
int[] years = new int[5];
for (int i = (DateTime.Now.Year - 2), j = 0; i <= (DateTime.Now.Year + 2); i++, j++)
{
years[j] = i;
}
ViewBag.Years = years;
ViewBag.Period = period;
return View(cycleUpdate);
}
}
else
{
List<CyclePeriod> period = new List<CyclePeriod>{
new CyclePeriod {Id=1, Title="First Half : January 1 - June 30"},
new CyclePeriod {Id=2, Title="Second Half : July 1 - December 31"}
};
int[] years = new int[5];
for (int i = (DateTime.Now.Year - 2), j = 0; i <= (DateTime.Now.Year + 2); i++, j++)
{
years[j] = i;
}
ViewBag.Years = years;
ViewBag.Period = period;
return View(cycleUpdate);
}
}
catch (Exception)
{
List<CyclePeriod> period = new List<CyclePeriod>{
new CyclePeriod {Id=1, Title="First Half : January 1 - June 30"},
new CyclePeriod {Id=2, Title="Second Half : July 1 - December 31"}
};
int[] years = new int[5];
for (int i = (DateTime.Now.Year - 2), j = 0; i <= (DateTime.Now.Year + 2); i++, j++)
{
years[j] = i;
}
ViewBag.Years = years;
ViewBag.Period = period;
return View(cycleUpdate);
}
}
答案 0 :(得分:15)
我同意Tibi的观点。您可能想要使用可空类型。
“如何确定ModelState无效的原因?”
这将为您提供一个键列表(这是您的模型中的属性名称)及其来自ModelState的错误:
var errors = ModelState.Where(x => x.Value.Errors.Any())
.Select(x => new { x.Key, x.Value.Errors });
答案 1 :(得分:1)
如果年份和期间的类型为int,则表示它们不能为空(因此需要)。为了避免这种情况,你必须使用int? (Nullable)