MVC4型号状态无效
本文关键字:无效 状态 MVC4 | 更新日期: 2023-09-27 18:01:12
我在mvc4+EF6项目中发布时遇到问题。视图中的模型填充了除年份和期间之外的所有字段(它们是下拉列表字段(。在附加到视图的视图模型中,字段Year和Period未设置为DataAnnotations所必需。但是,当我尝试发布时,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);
}
}
我同意蒂比的观点。您可能希望使用可为null的类型。
"如何确定ModelState无效的原因?">
这将从ModelState:获得一个密钥列表(这是您模型中的属性名称(及其错误
var errors = ModelState.Where(x => x.Value.Errors.Any())
.Select(x => new { x.Key, x.Value.Errors });
如果年份和期间的类型为int,这意味着它们不能为null(因此是必需的(。为了避免这种情况,你必须使用int?(可为空(