我正在使用Visual Studio 2017社区版测试ASP.NET MVC 5应用程序。
我试图用以下代码将Assort
模型保存到数据库。我正在使用URL Assort Create
导航到/Assort/Create/1A
页面。在1A
的create
页面上需要参数Assort
,因为我需要在创建页面本身上显示该参数的一些附加信息。
但是当我提交数据时,1A
参数值被插入为ID
模型的Assort
值,因此我的ModelState
无效,我无法保存数据。
谁能帮我?
模型
public class Assort
{
[Key]
public int ID { get; set; }
[Display(Name = "Assort No")]
[Required(ErrorMessage = "Assort No can not be empty.")]
public int ASSORTNO { get; set; }
[Display(Name = "Date")]
[Required(ErrorMessage = "Date can not be empty.")]
public DateTime DATE { get; set; }
[Display(Name = "RFNO")]
[Required(ErrorMessage = "RFNO can not be empty.")]
[StringLength(50)]
public string RFNO { get; set; }
[Display(Name = "Manager")]
[Required(ErrorMessage = "Manager can not be empty.")]
public int MANAGER { get; set; }
[Display(Name = "Caret")]
[Required(ErrorMessage = "Caret can not be empty.")]
public decimal CARET { get; set; }
[Display(Name = "MFG Size")]
[Required(ErrorMessage = "MFG Size can not be empty.")]
public decimal MFGSIZE { get; set; }
[Display(Name = "Total PCS")]
[Required(ErrorMessage = "Total PCS can not be empty.")]
public decimal TOTALPCS { get; set; }
[StringLength(50)]
public string APPROVALSTATUS { get; set; }
[Display(Name = "Details")]
public string DETAILS { get; set; }
[ScaffoldColumn(false)]
public DateTime CREATE_TIMESTAMP { get; set; }
[ScaffoldColumn(false)]
public DateTime LAST_EDIT_TIMESTAMP { get; set; }
[UIHint("AssortReturn")]
public virtual List<AssortReturn> AssortReturn { get; set; }
public Assort()
{
AssortReturn = new List<AssortReturnModel.AssortReturn>();
}
[ForeignKey("RFNO")]
public virtual Rough rough { get; set; }
}
行动
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Assort assort)
{
if (ModelState.IsValid)
{
assort.APPROVALSTATUS = "NOT APPROVED";
assort.CREATE_TIMESTAMP = DateTime.Now;
assort.LAST_EDIT_TIMESTAMP = DateTime.Now;
db.Assorts.Add(assort);
db.SaveChanges();
return RedirectToAction("Index");
}
Initialize(assort.RFNO,"CREATE");
return View(assort);
}
视图
@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.ASSORTNO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ASSORTNO, new { htmlAttributes = new {@readonly="readonly",@Value=ViewBag.ASSORTNO, @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ASSORTNO, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DATE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DATE, new { htmlAttributes = new {@autofocus="autofocus",@Value=ViewBag.CURRENTDATE, @class = "form-control date" } })
@Html.ValidationMessageFor(model => model.DATE, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RFNO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RFNO, new { htmlAttributes = new { @readonly = "readonly", @Value = ViewBag.RFNO, @class = "form-control" } })
@Html.TextBox("AVAILABLECARET",(decimal)ViewBag.AVAILABLECARET,new {@class="form-control txtAvailablecaret",@readonly="readonly" })
@Html.ValidationMessageFor(model => model.RFNO, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MANAGER, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.MANAGER, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.DropDownListFor(model => model.MANAGER, new SelectList(ViewBag.MANAGERLIST, "ID", "USERNAME"), "Select Manager", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MANAGER, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CARET, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CARET, new { htmlAttributes = new { @class = "form-control txtCaret" } })
@Html.ValidationMessageFor(model => model.CARET, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MFGSIZE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MFGSIZE, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MFGSIZE, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TOTALPCS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TOTALPCS, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TOTALPCS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DETAILS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DETAILS, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DETAILS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default btnCreate" />
</div>
</div>
</div>
}
这是因为默认路由正在处理您的请求。看起来像:
{controller}/{action}/{id}
所以A1绑定了ID。如果你想要一个不同的行为,比如说A1仍然是URL的一部分,但是绑定到另一个参数,比如“name”,你需要一个新的路径:
routes.MapRoute(
name: "CreateAssort",
url: "Assort/Create/{name}",
defaults: new { controller = "Assort", action = "Create"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
现在“name”将保存A1而不是ID。请注意您的自定义路由如何在默认路由之前。这很重要 - 路由选择匹配请求的第一条路由。
你可以做的是在你的视图中添加一个名为hidden
的ID
输入字段。
当提交表单时,此字段中的值将优先于您的路径中的值,即“1A”,如果您未设置隐藏输入的值,则模型将ID设置为0。