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 7655499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:31:49+00:00 2026-05-31T12:31:49+00:00

I am having problems getting my create function to work right. I am trying

  • 0

I am having problems getting my create function to work right. I am trying to create an Order object, which has a SalesPerson and Customer object in it. My order model looks like

public class Order
{
    public int ID { get; set; }
    public SalesPerson SalesPerson { get; set; }
    public bool PreviousWork { get; set; }
    public OrderStatus Status { get; set; }
    public Customer Customer { get; set; }
    public List<OrderLineItem> LineItems { get; set; }
}

I then created a view model:

public class OrderViewModel
{
    private sunburstdb db = new sunburstdb();

    public Order originalOrder { get; set; }
    public IList<SelectListItem> SalesPeopleList { get; set; }
    public IList<SelectListItem> CustomersList { get; set; }
    public IList<SelectListItem> OrderStatusList { get; set; }

    public OrderViewModel(Order order)
    {
        originalOrder = order;
    }
}

In my controller I have the following:

    //
    // GET: /Order/Create

    public ActionResult Create()
    {
        Order order = new Order();
        OrderViewModel viewModel = new OrderViewModel(order);
        //IList<SelectListItem> result = new List<SelectListItem>();
        viewModel.SalesPeopleList = new List<SelectListItem>();
        foreach (SalesPerson person in db.SalesPeople)
        {
            var temp = new SelectListItem();
            temp.Text = person.FullName;
            temp.Value = person.ID.ToString();
            viewModel.SalesPeopleList.Add(temp);
        }
        //viewModel.SalesPeopleList = new SelectList(result);
        //result.Clear();
        viewModel.CustomersList = new List<SelectListItem>();
        foreach (Customer person in db.Customers)
        {
            var temp = new SelectListItem();
            temp.Text = person.FullName;
            temp.Value = person.ID.ToString();
            viewModel.CustomersList.Add(temp);
        }
        //viewModel.CustomersList = new SelectList(result);

        return View(viewModel);
    }

    //
    // POST: /Order/Create

    [HttpPost]
    public ActionResult Create(Order order)
    {
        if (ModelState.IsValid)
        {
            db.Orders.Add(order);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(order);
    }

Finally my view is pretty standard with a couple of fields to populate the data in the order.

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Order</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.originalOrder.SalesPerson)
    </div>
    <div class="editor-field">
        @Html.DropDownList("Order.SalesPerson", Model.SalesPeopleList)
    </div>
            <div class="editor-label">
        @Html.LabelFor(model => model.originalOrder.Customer)
    </div>
    <div class="editor-field">
        @Html.DropDownList("Order.Customer", Model.CustomersList);
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.originalOrder.PreviousWork)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.originalOrder.PreviousWork)
        @Html.ValidationMessageFor(model => model.originalOrder.PreviousWork)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

When I run this and try to create a new order I get the following: The model item passed into the dictionary is of type ‘Models.Order’, but this dictionary requires a model item of type ‘Models.OrderViewModel’. I thought maybe I needed to change the parameter in the create method to public ActionResult Create(OrderViewModel order) however when I do this the error is: No parameterless constructor defined for this object. Can someone provide some help to an MVC Noob about what I am doing wrong?

  • 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-31T12:31:50+00:00Added an answer on May 31, 2026 at 12:31 pm

    in the action pass the viewmodel.
    The error you get is because you created only a constructor with parameters, but MVC wnat also a parameterless contructor.

    Aps.net 4 will create it automatically for you if you don’t specify any constructor, but if you define one, then it don’t take initiative creating one that maybe you don’t want.

    Look here. That should explain better than me

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

Sidebar

Related Questions

I am having problems getting the 'ByAccount' aggregation to work in SQL Server Analysis
I am having problems getting validation to work for a form built using Zend_Form.
I'm having problems getting my game engine to run on my brother's machine, which
Hi I'm having problems getting this to work in mysql. DELIMITER $$ DROP PROCEDURE
I'm having problems trying to call a Javascript function from an enqueued javascript file
I am having problems with a line of code. I am trying to create
I'm having troubles getting a regex to work. I'm trying to parse a large,
I come from a C++/Java background, but I'm having problems getting the syntax right
I'm trying to create this JSON string but having problem creating it. I'm getting
I am having problems getting text within a table to appear centered in IE.

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.