我是MVC的新手。我有一个父视图,可以让用户为客户创建报价。一个AJAX链接,“添加产品”加载一个局部视图“EditQuoteDetail”,使用户可以将产品添加到报价中。问题是,如果ModelState在POST上无效(例如用户忘记输入所需的电话号码),则产品的部分视图不会返回到视图。我必须做什么才能将QuoteDetails返回给用户?如果模型状态无效,部分视图不被保留
这里是主视图:
@model CMSUsersAndRoles.Models.QuoteViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="~/Scripts/jquery.mask.min.js"></script>
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Quote</h4>
<hr />
<div class="form-group">
<div class="col-md-10">
@Html.HiddenFor(model => model.QuoteId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Company, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CustomerId, new SelectList(ViewBag.Customers, "CustomerId", "Company"), "---Select one---", new { style = "width: 300px !important", htmlAttributes = new { @class = "company" } });
@Html.HiddenFor(model => model.Company, new { @class = "companyName" })
@Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" })
</div>
</div>
... //other Quote fields
<div class="form-group">
@Html.LabelFor(model => model.QuoteDetail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10" id="QuoteDetails">
@Html.ValidationMessageFor(model => model.QuoteDetail, "", new { @class = "text-danger"})
@Ajax.ActionLink("Add product", "AddProduct", "QuoteViewModel", new { quoteId = Model.QuoteId, quoteDetailId = (Model.QuoteDetail.Count + 1) }, new AjaxOptions
{
UpdateTargetId = "QuoteDetails",
InsertionMode = InsertionMode.InsertBefore
})
</div>
下面是部分视图:
@model CMSUsersAndRoles.Models.QuoteDetail
@{
ViewBag.Title = "EditQuoteDetail";
Layout = null;
}
<div id="row" class="row">
<table>
@using (Html.BeginCollectionItem("quoteDetail"))
{
<tr>
@Html.HiddenFor(model => model.QuoteId, new { htmlAttributes = new { @class = "form-control" } })
@Html.HiddenFor(model => model.QuoteDetailId, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.SKU, new { htmlAttributes = new { @readonly = "readonly", @id = "SKU", @class = "form-control", style = "width: 100px" } })
@Html.DropDownListFor(model => model.ProductId, new SelectList(ViewBag.ProductData, "ProductId", "Name"), "---Select one---", new { style = "width: 300px !important", required = "required", htmlAttributes = new { @id = "ProductName", @class = "ProductList" } })
@Html.HiddenFor(model => model.ProductName)
@Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @id = "Amt", @class = "form-control amount", style = "width: 95px" } })
@Html.EditorFor(model => model.ListPrice, new { htmlAttributes = new { @readonly = "readonly", @id = "LPrce", @class = "form-control listprice", style = "width: 95px" } })
@Html.EditorFor(model => model.Discount, new { htmlAttributes = new { @id = "TotalDiscount", @class = "form-control discount", style = "width: 100px" } })
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @readonly = "readonly", @id = "FinalPrce", @class = "form-control price", style = "width: 100px" } })
@Ajax.ActionLink(" ", "DeleteProduct", "QuoteViewModel", new { quoteId = Model.QuoteId, quoteDetailId = (Model.QuoteDetailId) },
new AjaxOptions
{
HttpMethod = "POST",
Confirm = "Are you Sure You Want to Delete " + Model.ProductName,
OnSuccess = "RemoveRow"
},
new { @class = "btn btn-danger glyphicon glyphicon-trash" })
</tr>
}
</table>
</div>
这里是POST:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(QuoteViewModel qvm)
{
if (qvm.QuoteDetail == null)
{
qvm.QuoteDetail = new List<QuoteDetail>();
qvm.QuoteDetail.Add(new QuoteDetail() { QuoteId = qvm.QuoteId, QuoteDetailId = (qvm.QuoteDetail.Count + 1) });
var customerList = db.Customers.ToList();
ViewBag.Customers = customerList;
return View(qvm);
}
if (ModelState.IsValid)
{
... //process
return RedirectToAction("Index");
}
var customers = db.Customers.ToList();
ViewBag.Customers = customers;
return View(qvm);
}
这里是加载的局部代码查看:
public ActionResult AddProduct(int quoteId, int quoteDetailId)
{
var items = db.Products.ToList();
ViewBag.ProductData = items;
return PartialView("EditQuoteDetail", new QuoteDetail { QuoteId = quoteId, QuoteDetailId = quoteDetailId });
}
这里是视图模型的相关章节:
public class QuoteViewModel
{ // Columns from QuoteDetail table
[Required(ErrorMessage ="Please add product(s) to quote ")]
[Display(Name = "Quote Detail")]
public List<QuoteDetail> QuoteDetail { get; set; }
}
我缺少什么?任何帮助都感激不尽。
你需要证明你的'QuoteViewModel模型' - 它是否包含一个名为'quoteDetail'的属性,它是'QuoteDetail'的集合? –
@StephenMuecke,上面添加。 –