【发布时间】:2018-06-04 03:42:14
【问题描述】:
我正在使用 Visual Studio 2017 社区版测试 ASP.NET MVC 5 应用程序。
我正在尝试使用以下代码将Assort 模型保存到数据库。
我正在导航到带有 URL /Assort/Create/1A 的 Assort Create 页面。
在Assort 的create 页面上需要参数1A,因为我需要在创建页面本身上显示该参数的一些附加信息。
但是当我提交数据时,1A 参数值被插入为Assort 模型的ID 值,因此我的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>
}
【问题讨论】:
-
为什么不发送“1A”作为
ID以外的参数?喜欢create?param1=1A -
@sachin 如果我想保持干净的 URL 怎么办?还有其他建议吗?
-
所以你想要A1,但是作为不同参数的值,没有ID?
-
@Andrei,是的,我想要那个。
-
您正在编辑数据,因此您应该始终使用视图模型,并且该视图模型可以包含一个属性(例如)
public int AssortID { get; set; },因此它不会自动与路由值绑定。
标签: asp.net-mvc routes modelstate