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

  • Home
  • SEARCH
  • 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 8187913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:43:35+00:00 2026-06-07T02:43:35+00:00

I wish to show a DropDownList in a view and therfore include in my

  • 0

I wish to show a DropDownList in a view and therfore include in my model (ExampleAddSetupDto) sent to a view a list of entries to populate the dropdownlist. That works fine, but if I have a validation error and redisplay the view with in incoming model my list is now null.

My Action is given below (note: the problem occurs if ModelState.IsValid fails). Also the Action method second parameter may look odd as I am using Autofac to inject the right service into the method).

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Add(ExampleAddSetupDto add, IServiceAddCommit<IExampleAddSetupDto, IExampleAddCommitDto> service)
    {

        if (ModelState.IsValid)
        {
            var response = service.Create(add);
            if (response.IsValid)
            {
                TempData["message"] = "You successfully added a new Example Entry";
                return View("AddSuccess", response);
            }

            //else errors, so copy the errors over to the ModelState
            response.CopyErrorsToModelState(ModelState, add);
        }

        // Some validation error, so redisplay same view
        return View(add);

    }

My model looks like this:

public class ExampleAddSetupDto : IExampleAddSetupDto
{

    [StringLength(50, MinimumLength = 2)]
    public string Name { get; set; }

    public int Option1Id { get; set; }

    public int Option2Id { get; set; }

    //-----------------------------
    //now the properties for the drop down lists 

    public IList<Option1> PosibleEntriesForOption1 { get; set; }
    public IList<Option2> PosibleEntriesForOption2 { get; set; }

}

My View is:

@model ServiceLayer.Example.DTOs.ExampleAddSetupDto

@{
    ViewBag.Title = "Add";
}

<h2>Add</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.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
    <legend>Add an Example item</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.Label("Option1")
        @Html.DropDownListFor(model => model.Option1Id, new SelectList(Model.PosibleEntriesForOption1, "Option1Id", "OptionText"))
        @Html.ValidationMessageFor(model => model.Option1Id)
    </div>
    <div class="editor-field">
        @Html.Label("Option2")
        @Html.DropDownListFor(model => model.Option2Id, new SelectList(Model.PosibleEntriesForOption2, "Option2Id", "OptionText"))
        @Html.ValidationMessageFor(model => model.Option2Id)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

I understand that I need to return the Model.PosibleEntriesForOption in with the form. I tried using the Html.HiddenFor helper in the view to return the list, i.e.

@Html.HiddenFor(model => model.PosibleEntriesForOption1)

but this throws the error
‘The value ‘System.Collections.Generic.List`1[DataClasses.ExampleClasses.Option1]’ is invalid.’.

Clearly I am missing something here and I would appreciate some advice on how to return the lists so that redisplaying the model won’t cause an error.

  • 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-07T02:43:37+00:00Added an answer on June 7, 2026 at 2:43 am

    If you are forced to persist the entire list between the two requests, for whatever reason, the best way I see to do this would be to use:

    TempData["EnterUniqueKeyHere"] = PossibleEntriesForOption1;
    

    to store it, and then:

    PossibleEntriesForOption1 = TempData["EnterUniqueKeyHere"] as IList<Option1>;
    

    to retrieve it.

    Note that anything stored in TempData will be removed automatically after a single request.

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

Sidebar

Related Questions

I have a list of objects in a model. I wish to show elements
I wish to show my webpage logo and menu before adding a chart that
I wish to show tabs in an activity such that one of the tabs
I created carousel design application, it's working fine. I wish to show one default
I wish to show exit code on Bash shell prompt, so that I can
I wish to show items that I have queried with LINQ on to the
If I have a decision problem A, and wish to show that it is
I have a div element on my page that I wish to show/hide based
I am a newbie for iphone development, i wish to show some list of
I have a series of radio buttons that I wish to show a count

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.