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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:54:41+00:00 2026-05-20T02:54:41+00:00

This maybe very simple but I cant seem to sort it out on my

  • 0

This maybe very simple but I cant seem to sort it out on my own.
I have created a simple db and entity modal that looks like this

enter image description here

I am trying to create an Create form that allows me to add a new Order. I have a total of 3 tables so what I am trying to do is have the form allowing the person to enter Order date and also has a dropdown list that allows me to select a product from the product table

I want to be able to create a Add or Edit view that allow me to insert the OrderDate into the OrderTable and also insert the OrderID and selected ProductID into OrderProduct.

What steps do I need to do here.

I have created an OrderController and ticked the “Add Actions” and than added a Create View which looks like this

@model Test.OrderProduct

@{
    ViewBag.Title = "Create2";
}

    <h2>Create2</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>OrderProduct</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.OrderID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.OrderID)
            @Html.ValidationMessageFor(model => model.OrderID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductID)
            @Html.ValidationMessageFor(model => model.ProductID)
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

This creates the view that contains a textbox for both OrderID and ProductID however no date.

My controller CreatePost hasnt been changed

  [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            var data = collection;
            // TODO: Add insert logic here
          //  db.Orders.AddObject(collection);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

My questions are,

1.How do I swap out ProductID textbox to be a dropdown which is populated from Product
2.How do I get the data from FormCollection collection? I thought of just a foreach however I dont know how to get the strongly typed name

Any help for a newbie would be very helpful.

Thank you!

  • 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-20T02:54:41+00:00Added an answer on May 20, 2026 at 2:54 am

    First thing’s first, don’t bind to the Order entity. Never bind to an EF object, always try and use a ViewModel. Makes life simpler for the View, and that is the goal here.

    So, have a ViewModel like this:

    public class CreateOrderViewModel
    {
       public int OrderId { get; set; }
       public DateTime OrderDate { get; set; }
       public int SelectedProductId { get; set; }
       public IEnumerable<SelectListItem> Products { get; set; }
    }
    

    That’s it right now.

    Return that to your View in your [HttpGet] controller action:

    [HttpGet]
    public ActionResult Create()
    {
       var model = new CreateOrderViewModel
       {
          Products = db.Products
                       .ToList() // this will fire a query, basically SELECT * FROM Products
                       .Select(x => new SelectListItem
                        {
                           Text = x.ProductName,
                           Value = x.ProductId
                        });
       };
    
       return View(model);
    }
    

    Then to render out the list of Products: (basic HTML excluded)

    @model WebApplication.Models.CreateOrderViewModel
    
    @Html.DropDownListFor(model => model.SelectedProductId, Model.Products)
    

    The only thing i don’t know how to do is bind to the DateTime field. I’m guessing you would need an extension method (HTML Helper) which renders out a Date Picker or something. For this View (creating a new order), just default to DateTime.Now.

    Now, onto the [HttpPost] controller action:

    [HttpPost]
    public ActionResult Create(CreateOrderViewModel model)
    {
       try
       {
          // TODO: this manual stitching should be replaced with AutoMapper
          var newOrder = new Order
          {
             OrderDate = DateTime.Now,
             OrderProduct = new OrderProduct
             {
                ProductId = SelectedProductId
             }
          };
    
          db.Orders.AddObject(newOrder);
          return RedirectToAction("Index");
       }
       catch
       {
          return View();
       }
    }
    

    Now, i also think your EF model needs work.

    To me (in English terms), a Product can have many orders, and an Order can have many Products.

    So, it should be a many-to-many. Currently it’s a 1-1 with a redundant join table. Did you generate that from a DB? If so, your DB possibly needs work.

    You should have a navigational property called Products on the Order entity, which references a collection of Product, made possible by a silent join to the join table in the many-to-many.

    This also means you no longer have a DropDownList, but a MultiSelectDropDownList.

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

Sidebar

Related Questions

Maybe someone can help me out. I have created a simple web user control,
I have this strange, yet maybe very simple problem.. I have a timer1 in
Maybe this is a very simple question, but I can't find the answer: How
This may be very simple question,But please help me. i wanted to know what
This may be a very simple problem, but I couldn't find an answer googleing
this may be a very simple question, but is it possible to force a
Maybe a very simple question. How can I put in this code <Query> <Where>
I know this maybe a very basic question but I'm having a bit of
Maybe this is something very easy to do it but so far it's taking
I think this is a very simple question, but I’m new to programming so

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.