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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:07:46+00:00 2026-06-02T17:07:46+00:00

In my application I have a drop down list to represent different choices. Note

  • 0

In my application I have a drop down list to represent different choices. Note that Paragraph is a model and the section is just one field in the model.

@Html.DropDownList("Sections")

And here is my controller.

public ActionResult Edit(int id)
{
    var paragraph = db.Paragraphs.Find(id);

    ViewBag.Sections = new SelectList(
        db.Sections.Select(s => new { s.ID, s.Name }),
        "ID", "Name", paragraph.SectionID
    );

    return View(paragraph);
}

[HttpPost]
public ActionResult Edit(Paragraph paragraph, HttpPostedFileBase document)
{
    if (ModelState.IsValid)
    {
        // Do some stuff.
    }

    ViewBag.Sections = new SelectList(
        db.Sections.Select(s => new { s.ID, s.Name }),
        "ID", "Name", paragraph.SectionID
    );

    return View(paragraph);
}

When I submit the form though the drop down list is not bound to the model. Causing ModelState.IsValid to be false and making my life horrible. Any suggestions?

EDIT: When I submit the form I get the following error:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Sections'.

EDIT: It appears that I only get the preceding error when I try to submit the file.

EDIT: Model

public class Paragraph
{
    public int ID { get; set; }

    [Required]
    public int Major { get; set; }

    [Required]
    public int Minor { get; set; }

    [Required(ErrorMessage = "Name is required")]
    [StringLength(4000)]
    public string Name { get; set; }

    public int SectionID { get; set; }
    public virtual Section Section { get; set; }
}

Form: (It’s a lot.)

<form class="form-horizontal" action="/Paragraph/Edit" method="post" enctype="multipart/form-data">
    <fieldset>
        <div class="control-group">
            <label class="control-label" for="section">Section</label>
            <div class="controls">
                @Html.DropDownList("Sections")
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="major">Major</label>
            <div class="controls">
                <input type="number" class="input-large" name="major" value="@Model.Major" />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="minor">Minor</label>
            <div class="controls">
                <input type="number" class="input-large" name="minor" value="@Model.Minor" />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="name">Name</label>
            <div class="controls">
                <input type="text" class="input-large" name="name" value="@Model.Name" />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="document">Document</label>
            <div class="controls">
                <input type="file" class="input-file" name="document" />
            </div>
        </div>
        <div class="form-actions">
            <input type="submit" class="btn btn-primary" value="Save" />
            <a class="btn" href="/Paragraph/Show/@Model.ID">Cancel</a>
        </div>
    </fieldset>
</form>
  • 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-06-02T17:07:48+00:00Added an answer on June 2, 2026 at 5:07 pm

    This is late, but better late than never. When you use one of the helpers provided by HtmlHelper it generates the input name behind the scenes. The generated format for this name is Model.Field. For example, a paragraph with a field called “Name” would become Paragraph.Name.

    The definition for DropDownList that accepts one parameter uses the name of the drop-down list as the name of the input though. So, a call to DropDownList("Sections") would create an input with a name of “Sections.”

    In the end though, I decided to concede to Razor and use the built-in HTML helpers. So my drop-down list code looks like: DropDownListFor(vm => vm.Paragraph.SectionID, Models.Sections).

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

Sidebar

Related Questions

I have an internal application that I needs to have a drop down list
I have this application that has a form with a drop-down list and a
I am doing a yii web application i have a drop down list that
In my application I have a drop-down form that fires up an AJAX request
I'd like to add a drop-down list to a Windows application. It will have
I have one radio button list and one drop down list inside the modal
In my application i have two drop down boxes first box had origin second
I have a drop down list , in which i have list of browsers
One of the drop down lists in my Java SWT application has 8 fixed
in my Application i have text box beside it i have drop down control.

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.