无法通过使用 ajax 更新的视图和分部视图创建模型
Can't create Model by View and Partialview updated using ajax
首先对不起我的英语。
我的问题:
我有一个名为 Inspeccio 的 table 和一个名为 Criteris 的相关 table。关系是 ManyToMany 所以我有 table CriterisInspeccio.
我有这些 table 同名的模型。
当我想创建一个 Inspeccio 时,有一个用于不同类型 Inspeccio 的下拉列表。
当我更改此下拉列表的值时,使用 ajax 更新了 Criteris 列表。
但是当我提交表单时,控制器会收到没有列表 Criteris 的 InspeccioViewModel。
代码:
InspecciosController:
// GET: Inspeccios/Create
public ActionResult Create()
{
ViewBag.IdTipusInspeccio = new SelectList(db.TipusInspeccio, "IdTipusInspeccio", "Tipus");
InspeccioViewModel inspeccioViewModel = new InspeccioViewModel();
return View(inspeccioViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(InspeccioViewModel inspeccioViewModel)
{
if (ModelState.IsValid)
{
Inspeccio inspeccio = InspeccioHelper.ToInspeccio(inspeccioViewModel);
db.Inspeccio.Add(inspeccio);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(inspeccioViewModel);
}
public ActionResult CriterisSegonsTipusInspeccio(int idTipusInspeccio)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(idTipusInspeccio);
return PartialView("CriterisInspeccioAssignatsViewModel", criteris);
}
创建视图:
@model Inspeccions.ViewModels.InspeccioViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Inspeccio</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Clau, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Clau, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Clau, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataCaducitat, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataCaducitat, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.DataCaducitat, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Data, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Data, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.Data, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Hora, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Hora, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Hora, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Expedient, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Expedient, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Expedient, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IdTipusInspeccio, "Tipus", htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("IdTipusInspeccio", null, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.IdTipusInspeccio, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group" id="criteris"></div>
<div>
<br />
</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("Tornar al llistat d'inspeccions", "Index")
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#IdTipusInspeccio").change(function () {
var categoryId = $("#IdTipusInspeccio").val();
$("#criteris").load('@(Url.Action("CriterisSegonsTipusInspeccio", "Inspeccios", null, Request.Url.Scheme))?idTipusInspeccio=' + categoryId);
});
});
</script>
PartialView CriterisInspeccioAssignatsViewModel:
@model ICollection<Inspeccions.ViewModels.CriterisInspeccioAssignatsViewModel>
@using Inspeccions.Helpers
@foreach (var criteri in Model)
{
<div class="form-horizontal">
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(criter => criteri.Titol)
</dt>
<dd>
@Html.DisplayFor(criter => criteri.Titol)
</dd>
<dt>
@Html.DisplayNameFor(criter => criteri.Descripcio)
</dt>
<dd>
@Html.DisplayFor(criter => criteri.Descripcio)
</dd>
</dl>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.HiddenFor(criter => criteri.IdCriteri, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(criter => criteri.IdCriteri, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(criter => criteri.IdCriteri, "", new { @class = "text-danger" })
</div>
</div>
@if (@Html.Action() == "Create" || @Html.Action() == "Edit" || @Html.Action() == "CriterisSegonsTipusInspeccio")
{
<div class="form-group">
@Html.LabelFor(criter => criteri.Assignada, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(criter => criteri.Assignada)
@Html.ValidationMessageFor(criter => criteri.Assignada, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
@if (@Html.Action() == "Details")
{
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(criter => criteri.Assignada)
</dt>
<dd>
@Html.DisplayFor(criter => criteri.Assignada)
</dd>
</dl>
}
</div>
<hr />
}
如果我在没有 ajax 的情况下使用它,修复 Criteris 就可以了...
我已经在 Google 中搜索过了,但找不到适合我的解决方案。
你能帮帮我吗?
非常感谢!
我终于找到了解决办法!
问题来自
public ActionResult CriterisSegonsTipusInspeccio(InspeccioViewModel inspeccioViewModel)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(inspeccioViewModel.IdTipusInspeccio);
inspeccioViewModel.CriterisInspeccioViewModel = criteris;
return PartialView("CriterisInspeccioAssignatsViewModel", inspeccioViewModel);
}
它必须接收模型:)
首先对不起我的英语。
我的问题:
我有一个名为 Inspeccio 的 table 和一个名为 Criteris 的相关 table。关系是 ManyToMany 所以我有 table CriterisInspeccio.
我有这些 table 同名的模型。
当我想创建一个 Inspeccio 时,有一个用于不同类型 Inspeccio 的下拉列表。
当我更改此下拉列表的值时,使用 ajax 更新了 Criteris 列表。
但是当我提交表单时,控制器会收到没有列表 Criteris 的 InspeccioViewModel。
代码:
InspecciosController:
// GET: Inspeccios/Create
public ActionResult Create()
{
ViewBag.IdTipusInspeccio = new SelectList(db.TipusInspeccio, "IdTipusInspeccio", "Tipus");
InspeccioViewModel inspeccioViewModel = new InspeccioViewModel();
return View(inspeccioViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(InspeccioViewModel inspeccioViewModel)
{
if (ModelState.IsValid)
{
Inspeccio inspeccio = InspeccioHelper.ToInspeccio(inspeccioViewModel);
db.Inspeccio.Add(inspeccio);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(inspeccioViewModel);
}
public ActionResult CriterisSegonsTipusInspeccio(int idTipusInspeccio)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(idTipusInspeccio);
return PartialView("CriterisInspeccioAssignatsViewModel", criteris);
}
创建视图:
@model Inspeccions.ViewModels.InspeccioViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Inspeccio</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Clau, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Clau, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Clau, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataCaducitat, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataCaducitat, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.DataCaducitat, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Data, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Data, new { htmlAttributes = new { @class = "form-control date-picker" } })
@Html.ValidationMessageFor(model => model.Data, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Hora, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Hora, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Hora, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Expedient, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Expedient, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Expedient, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IdTipusInspeccio, "Tipus", htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownList("IdTipusInspeccio", null, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.IdTipusInspeccio, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group" id="criteris"></div>
<div>
<br />
</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("Tornar al llistat d'inspeccions", "Index")
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#IdTipusInspeccio").change(function () {
var categoryId = $("#IdTipusInspeccio").val();
$("#criteris").load('@(Url.Action("CriterisSegonsTipusInspeccio", "Inspeccios", null, Request.Url.Scheme))?idTipusInspeccio=' + categoryId);
});
});
</script>
PartialView CriterisInspeccioAssignatsViewModel:
@model ICollection<Inspeccions.ViewModels.CriterisInspeccioAssignatsViewModel>
@using Inspeccions.Helpers
@foreach (var criteri in Model)
{
<div class="form-horizontal">
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(criter => criteri.Titol)
</dt>
<dd>
@Html.DisplayFor(criter => criteri.Titol)
</dd>
<dt>
@Html.DisplayNameFor(criter => criteri.Descripcio)
</dt>
<dd>
@Html.DisplayFor(criter => criteri.Descripcio)
</dd>
</dl>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.HiddenFor(criter => criteri.IdCriteri, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(criter => criteri.IdCriteri, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(criter => criteri.IdCriteri, "", new { @class = "text-danger" })
</div>
</div>
@if (@Html.Action() == "Create" || @Html.Action() == "Edit" || @Html.Action() == "CriterisSegonsTipusInspeccio")
{
<div class="form-group">
@Html.LabelFor(criter => criteri.Assignada, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(criter => criteri.Assignada)
@Html.ValidationMessageFor(criter => criteri.Assignada, "", new { @class = "text-danger" })
</div>
</div>
</div>
}
@if (@Html.Action() == "Details")
{
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(criter => criteri.Assignada)
</dt>
<dd>
@Html.DisplayFor(criter => criteri.Assignada)
</dd>
</dl>
}
</div>
<hr />
}
如果我在没有 ajax 的情况下使用它,修复 Criteris 就可以了...
我已经在 Google 中搜索过了,但找不到适合我的解决方案。
你能帮帮我吗?
非常感谢!
我终于找到了解决办法!
问题来自
public ActionResult CriterisSegonsTipusInspeccio(InspeccioViewModel inspeccioViewModel)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(inspeccioViewModel.IdTipusInspeccio);
inspeccioViewModel.CriterisInspeccioViewModel = criteris;
return PartialView("CriterisInspeccioAssignatsViewModel", inspeccioViewModel);
}
它必须接收模型:)