我有一个剃刀视图,有表格数据。当我提交表单时,我想在“表单”模型中输入一列(ID),并将整个表单数据传递给“初始化”模型。mvc剃刀贴到2个模型
View:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
模型1:初始化
public partial class Init
{
public int Id { get; set; }
public string name{ get; set; }
}
模型2:表
public partial class Form
{
public int Id { get; set; }
public int field2 { get; set; }
public int field3 { get; set; }
}
控制器:
public ActionResult Create([Bind(Include = "Id,Name")] Init init)
{
if (ModelState.IsValid)
{
db.Init.Add(init);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(init);
}
在视觉studiio,当你创建一个视图,选择与模型强类型,到你想要的数据的选项。 –
我拥有的视图强制输入到Init模型,并在提交它时将数据添加到Init模型。我该如何将表单中的Id值添加到另一个模型(表单模型)? – user7221204
什么是“DocId”,“供应商”,“供应商文字”?我没有看到属性不在模型中,也不在视图中。 – adamshakhabov