2010-07-18 34 views
0

我试图将相同的模型返回到编辑模型的视图。使它有点像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> 
+0

你可以发表你的看法吗? – ZippyV 2010-07-18 11:23:52

+0

首先,您正在讨论模型绑定器的问题,而不是填充对象的属性,然后发布视图中发生的异常,因为它没有正确键入,您想知道什么? – 2010-07-18 11:29:14

+0

@ZippyV - 我发布了这个视图,虽然我没有看到这是如何有什么兴趣,因为它是从编辑(诠释我)的行动结果工作。 – mhenrixon 2010-07-18 11:33:49

回答

1

为什么只有Invoice属性填充是因为在你的表格你只具有其输入字段和下拉列表的原因。模型绑定器根据请求中发送的内容填充属性。您发布的表单仅包含Invoice的值。就WorkdaysProducts属性而言,您只会显示它们(Html.DisplayFor),并且它们永远不会发送到服务器。此外,模型联编程序也会调用您的模型的构造函数,它们既不会初始化这些属性,所以它们在回发时为空。

+0

好的,那回答我的问题。回到绘图板我想我必须为此创建一个新的模型绑定器。我可以返回Edit(int id)操作结果没有问题,但是如果服务器端验证失败或者我只是再次检索所需的数据,用户可能不得不重新输入更改。谢谢! – mhenrixon 2010-07-18 11:44:17

2

达林是正确的。请记住,您仍在使用断开连接的客户端。这只是HTML和HTTP下的内容。模型联编程序只能绑定推送到HTTP POST中服务器的值。所有其他属性的类将得不到分配,所以如果你想要一个更完整的模型在

return View(invoiceModel); 

推回给浏览器响应,你需要将控制器内完成服务器端的属性赋值或者使用您的存储库的更新方法。