首先抱歉我的英语。
我的问题:
我有一个名为Inspeccio的表和一个名为Criteris的相关表。关系是ManyToMany所以我有表CriterisInspeccio。
我为这些具有相同名称的表创建了一个模型。
当我想创建一个Inspeccio时,会有一个针对不同类型的Inspeccio的下拉列表。
当我更改此Dropdownlist的值时,使用ajax更新Criteris列表。
但是当我提交表单时,控制器会收到InspeccioViewModel而没有列表的Criteris。
代码:
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修复就可以了......
我在谷歌搜索它,我找不到适合我的解决方案。
你能帮我吗?
非常感谢!
答案 0 :(得分:0)
我终于找到了解决方案!
问题来自
public ActionResult CriterisSegonsTipusInspeccio(InspeccioViewModel inspeccioViewModel)
{
ICollection<CriterisInspeccioAssignatsViewModel> criteris = CriterisSegonsTipusInspeccioFalse(inspeccioViewModel.IdTipusInspeccio);
inspeccioViewModel.CriterisInspeccioViewModel = criteris;
return PartialView("CriterisInspeccioAssignatsViewModel", inspeccioViewModel);
}
必须收到模型:)