我试图查看两个单独的表,但在同一视图中它们之间没有关系。这个想法是创建一个帐户并将其添加到帐户表中,并添加多个吊球并将其添加到吊球表中。所以我为每个视图制作了两个局部视图。我制作了一个ViewModel,它有一个帐户实例和lob列表。问题出在主视图中,局部模型返回null。我不知道为什么有任何想法吗? 感谢您的帮助。
@model Manage_account.Models.AccountVM
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div>
@{Html.RenderPartial("~/Views/AccountOrOU/PartialViews/_Account.cshtml", Model);}
</div>
<div>
@{Html.RenderPartial("~/Views/AccountOrOU/PartialViews/_Lob.cshtml", Model);}
</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>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
局部视图:
@model Manage_account.Models.AccountOrOU
@using (Html.BeginForm())
{
<div class="form-horizontal" id="ViewData">
<h4>AccountOrOU</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-10">
@Html.HiddenFor(model => model.AccountOrOUID, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AccountOrOUName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AccountOrOUName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AccountOrOUName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DepartmentID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DepartmentID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DepartmentID, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
型号:
public class AccountVM
{
public AccountOrOU AccountOrOU { get; set; }
public List<Lob> lobs { get; set; }
}
控制器:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AccountVM vM)
{if (vM != null)
{
var myAccount = db.AccountOrOUs.ToList();
foreach (var acc in myAccount)
{
acc.AccountOrOUID= vM.AccountOrOU.AccountOrOUID ;
acc.AccountOrOUName=vM.AccountOrOU.AccountOrOUName ;
acc.DepartmentID=vM.AccountOrOU.DepartmentID ;
db.AccountOrOUs.Add(acc);
}
db.SaveChanges();
}
return View("Index");
}
答案 0 :(得分:1)
我刚刚删除了部分ViewData.Model.AccountOrOU
并添加了Model
,它起作用了?。但老实说,我不知道这里的区别。第一个我曾经使用过并且正在工作。我不知道为什么不参与这个项目?