我试图将相同的模型返回到编辑模型的视图。使它有点像Word或某些与ctrl + s功能保存模式。这工作正常,虽然返回到视图的模型包含一堆愚蠢的理由。这是因为当控制器获得视图模型或者我以错误的方式处理MVC时,事情没有正确序列化?从编辑返回ViewModel的问题
这是Model
public class EditInvoiceModel
{
private readonly IEnumerable<Customer> _customers;
public EditInvoiceModel()
{
CreateProduct = new Product { Invoice = Invoice };
CreateWorkday = new Workday { Invoice = Invoice };
}
public EditInvoiceModel(Invoice invoice, IEnumerable<Customer> customers)
{
Invoice = invoice;
_customers = customers;
Customers = _customers.Select(x =>
new SelectListItem
{
Selected = x.Id == Invoice.CustomerID,
Text = x.Name,
Value = x.Id.ToString()
});
Products = Invoice.Products;
Workdays = Invoice.Workdays;
CreateProduct = new Product {Invoice = Invoice};
CreateWorkday = new Workday { Invoice = Invoice };
}
public IEnumerable<SelectListItem> Customers { get; set; }
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Workday> Workdays { get; set; }
public Product CreateProduct { get; set; }
public Workday CreateWorkday { get; set; }
public Invoice Invoice { get; set; }
}
这是控制器动作返回模型回到了同样的观点。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(EditInvoiceModel invoiceModel)
{
try
{
_repository.UpdateInvoice(invoiceModel.Invoice);
}
catch (Exception ex)
{
Log.Error(ex);
}
return View(invoiceModel);
}
当发票返回到视图时,除发票之外的所有属性均为空。我不知道为什么会发生这种情况。希望有人能帮忙。
发生的问题(在视图中)如下:这不是因为错字,因为它在第一次运行视图时工作正常。这必须是模型绑定器或模型绑定器使用的问题。
The ViewData item that has the key 'Invoice.CustomerID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The ViewData item that has the key 'Invoice.CustomerID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
Source Error:
Line 28: <div class="editor-field">
Line 29: <%: Html.DropDownListFor(x => x.Invoice.CustomerID, Model.Customers)%>
Line 30: <%: Html.ValidationMessageFor(x => x.Invoice.CustomerID)%>
Line 31: </div>
最后一部分显示视图模型的视图。
<%@ page language="C#" masterpagefile="~/Views/Shared/Site.Master"
inherits="System.Web.Mvc.ViewPage<FakturaLight.WebClient.Models.EditInvoiceModel>" %>
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server">
<%= Html.ValidationSummary() %>
<% using (Html.BeginForm())
{ %>
<%= Html.AntiForgeryToken() %>
<div class="content-left">
<%: Html.EditorFor(x => x.Invoice) %>
<div class="editor-label">
<%: Html.LabelFor(x => x.Invoice.CustomerID)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(x => x.Invoice.CustomerID, Model.Customers)%>
<%: Html.ValidationMessageFor(x => x.Invoice.CustomerID)%>
</div>
<% } %>
</div>
<div class="content-right" id="details" style=" clear:both;">
<div id="workdays">
<%: Html.DisplayFor(x => x.Workdays) %>
</div>
<div id="products">
<%: Html.DisplayFor(x => x.Products) %>
</div>
</div>
</asp:content>
你可以发表你的看法吗? – ZippyV 2010-07-18 11:23:52
首先,您正在讨论模型绑定器的问题,而不是填充对象的属性,然后发布视图中发生的异常,因为它没有正确键入,您想知道什么? – 2010-07-18 11:29:14
@ZippyV - 我发布了这个视图,虽然我没有看到这是如何有什么兴趣,因为它是从编辑(诠释我)的行动结果工作。 – mhenrixon 2010-07-18 11:33:49