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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:22:17+00:00 2026-05-29T15:22:17+00:00

In my viewmodel, I have a list of items I fetch from the database

  • 0

In my viewmodel, I have a list of items I fetch from the database and then send to the view. I would like to know if it’s possible to avoid having to refill the options property whenever I hit a Post action and need to return the model (for validation errors and what not)?

In webforms, this wouldn’t be necessary.

Edit: I was not clear. My problem is with the SelectList options I use for my DropDownLists. Everything gets posted, but if I have to return to the view (model is invalid), I have to reload the options from the database! I want to know if this can be avoided.

My viewmodel:

public class TestModel
{
    public TestModel()
    {
        Departments = new List<SelectListItem>();
    }

    public string Name { get; set; }
    public int Department { get; set; }
    public IEnumerable<SelectListItem> Departments { get; set; }
}

My view:

@model MvcApplication1.Models.TestModel    
@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Name)

    @Html.DropDownListFor(m => m.Department, Model.Departments)
    
    <input type=submit value=Submit />
}

My controller (do notice the comment on HttpPost):

public ActionResult Index()
{
    TestModel model = new TestModel
    {
        Name = "Rafael",
        Department = 1,
        Departments = new List<SelectListItem>
        {
            new SelectListItem { Text = "Sales", Value = "1" },
            new SelectListItem { Text = "Marketing", Value = "2", Selected = true },
            new SelectListItem { Text = "Development", Value = "3" }
        }
    };

    // Departments gets filled from a database.

    return View(model);
}

[HttpPost]
public ActionResult Index(TestModel model)
{
if (!ModelState.IsValid)
{
    //Do I have to fill model.Departments again!?!?!?
    
    return View(model); 
}
else { ...  }
}

Thanks in advance.

Edit: FYI, my solution was to use the Session variable.

  • 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-29T15:22:19+00:00Added an answer on May 29, 2026 at 3:22 pm

    Just need to strongly type your view, and change your controller method to have a parameter of that class type.

    That is, the view

    @model MyNamesspace.Models.MyModel
    ...
    @using (Html.BeginForm())
    {
        ....
    }
    

    And you controller method which is posted to.

    [HttpPost]
    public ActionResult MyAction(MyModel model)
    {
        ...
    }
    

    EDIT: Also make sure you have form fields for each property of the model which you need posted to the controller. My example is using Razor too BTW.

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

Sidebar

Related Questions

In my viewmodel, I have a list (ObservableCollection) containing items. In the view, this
I have a View which has a list of items bound to my ViewModel
I have a View / ViewModel where a ProductList is loaded. This list is
I have a list of items displayed as a ListBox. <ListView ItemsSource={Binding ListOfSomeItems}> <ListView.View>
I have a silverlight control (View) which displays a list of items in a
I have a view that has a list of items (which can be added
I have a view that contains a list of items, a TextBox and a
In my ViewModel I have a property, which is a list of models: private
I have a ViewModel class that contains a list of points, and I am
I have the following ViewModel public class RecommendationModel { public List<CheckBoxItem> CheckBoxList { get;

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.