代码之家  ›  专栏  ›  技术社区  ›  mhenrixon

从编辑返回ViewModel时出现问题

  •  0
  • mhenrixon  · 技术社区  · 14 年前

    我试图将相同的模型返回到编辑模型的视图中。使它有点像Word或者其他一些带有ctrl+s功能的东西来保存模式。尽管由于某些愚蠢的原因,返回到视图的模型包含了一堆空值,但这仍然可以正常工作。是因为当控制器取回视图模型时没有正确序列化,还是我处理MVC的方式不对?

    这就是模型

    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>
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Darin Dimitrov    14 年前

    为什么只有 Invoice 发票联 . 至于 Workdays Products Html.DisplayFor )它们永远不会被发送到服务器。模型绑定器还调用 您的模型的构造函数,它也不初始化这些属性,因此它们在回发时为空。

        2
  •  2
  •   Tyler Jensen    14 年前

    达林是对的。请记住,您仍在使用断开连接的客户端。这只是HTML和HTTP的封面下。模型绑定器只能绑定在httppost中推送到服务器的值。该类的所有其他属性将不会收到任何赋值,因此如果希望更完整的模型,请将其推回到浏览器中以响应

    return View(invoiceModel);
    

    您将需要在控制器内的服务器端或使用存储库的更新方法完成这些属性分配。