Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:35:27+00:00 2026-05-15T22:35:27+00:00

I trying to return the same model back to the view that edited the

  • 0

I trying to return the same model back to the view that edited the model. Making it sort of like Word or something with ctrl+s functionality for saving the mode. This works fine though the model that is returned to the view contains a bunch of nulls for some stupid reason. Is it because things were not serialized properly when the controller got the view model back or am I handling MVC the wrong way?

This is the 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; }
}

And this is the controller action that returns the model back to the same view.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(EditInvoiceModel invoiceModel)
{
    try
    {
        _repository.UpdateInvoice(invoiceModel.Invoice);
    }
    catch (Exception ex)
    {
        Log.Error(ex);
    }

    return View(invoiceModel);
}

All properties except the Invoice is null when this is returned to the view. I have no idea why this happens. Hope someone can help.

The problem that occurs (in the view) is the following: This is not because of a typo since it is working fine the first time the view is run. This must be to a problem with the modelbinder or my usage of the model binder.

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>

Lastly part of the view that displays the view model.

<%@ 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:35:28+00:00Added an answer on May 15, 2026 at 10:35 pm

    The reason why only the Invoice property is populated is because in your form you are only having input fields and dropdown lists for it. The model binder populates properties from what’s sent in the request. You are posting a form which contains values only for the Invoice. As far as the Workdays and Products properties are concerned you are only displaying them (Html.DisplayFor) and they are never sent to the server. Also the model binder invokes the default constructor of your model which doesn’t initialize those properties neither, so they are null at postback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to return the value that a $ajax call returns, from a function
Im trying to return a SimpleQuery list that queries a single table and uses
I'm trying to return use a JSON object with handlebars. Making a small todo
I am trying to create several models that all pull from the same table.
I am trying to model a sort of multiple inheritence relationship with EF 4.1
Shown below is the my custom table model. I am trying to use that
I'm trying to add a field to a Django model that will represent a
Am trying to return the sum of each day of a week in mysql
I'm trying to return the most common elements in a list (statistical mode). Unfortunately
I'm trying to return a left child as a pointer I have template <typename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.