【发布时间】:2016-03-22 19:00:20
【问题描述】:
Visual Studio 2015 更新 1。
MVC 5.2.3.
.NET 4.5.2
它可以选择显示名称,但它似乎不符合必需属性。谢谢!!!
查看:
@model Insure.Entities.Policy
@{ ViewBag.Title = "Policy"; }
<h2>Policy</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.EffDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EffDate)
@Html.ValidationMessageFor(model => model.EffDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ExpDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ExpDate)
@Html.ValidationMessageFor(model => model.ExpDate)
</div>
</div>
<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>
型号:
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Insure.Entities
{
public class Policy
{
public int PolicyID { get; set; }
public Guid PolicyNumber { get; set; }
[Required(ErrorMessage = "Effective Date Required")]
[DataType(DataType.DateTime)]
[DisplayName("Effective Date")]
public DateTime EffDate { get; set; }
[Required(ErrorMessage = "Expiration Date Required")]
[DataType(DataType.DateTime)]
[DisplayName("Expiration Date")]
public DateTime ExpDate { get; set; }
}
}
控制器:
// POST: Policy/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FormCollection collection)
{
try
{
if (ModelState.IsValid)
{
// TODO: Add logic to insert to DB
return RedirectToAction("Index");
}
else
{
return View();
}
}
catch
{
return View();
}
}
【问题讨论】:
-
public ActionResult Create(FormCollection collection)应该是public ActionResult Create(Policy myPolicyModel) -
添加它作为答案,我会这样标记它!
-
哦,对不起,我不是故意偷东西的。。直到我发帖后才看到这个
-
不用担心。一切都好!
-
@BviLLe_Kid - 不用担心。凯西 - 完成,谢谢。
标签: c# asp.net-mvc modelstate