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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:50:26+00:00 2026-05-18T10:50:26+00:00

I have created a viewmodel public VMPosition { public VMPosition(){}//for model binder public VMPosition(int

  • 0

I have created a viewmodel

public VMPosition
{
        public VMPosition(){}//for model binder  
        public VMPosition(int EmployeeID)
        {
            PositionStatusList = new SelectList(_repo.getStatuses);
            //populate other properties
        }
        public int CurrentPositionID { get; set; }
        public int EmployeeID { get; set; }
        public int CurrentPositionHistoryID { get; set; }
        public bool AddingNew { get; set; }
        public bool ClosingCurrent { get; set; }
        public string CurrentPosition { get; set; }
        public DateTime CurrentPositionStartDate { get; set; }
        public string ReasonForDeparture { get; set; }
        public SelectList PositionStatusList { get; set; }

}

My GET ActionResult is defined as

public ActionResult UpdatePosition(int id)
{
     return View(new VMPosition(id)); 
} 

My POST actionresult is defined as

 public ActionResult UpdatePosition(int id, VMPosition Position)
    {
        if(ModelState.IsValid){
             Position Current = new Position{Position.Title etc..}
             //save to db
             return redirectToAction("someAction");
         }
         return View(Position);//here is the problem
    }

My SelectList is populated in a constructor that accepts one parameter. Modelbinder cannot and should not call the constructor if the modelstate is invalid. I will have to return the View with model object (which in this case don’t contain SelectList value). How can handle this scenario when using view Models.

I can manually populate these values in actionresult but that will violate the DRY principle. However, for the purposes of this question, I’d like help addressing the bigger design question.

  • 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-18T10:50:27+00:00Added an answer on May 18, 2026 at 10:50 am

    When dealing with dropdowns in my viewmodels, I usually have a single property associated with the selected list item’s value and I have a property that returns a list of selectlistitems. Then, I use Html.DropDownListFor(m => m.ValueProperty, Model.DropDownValues) to render the dropdown.

    I imagine in your scenario, you don’t have a value corresponding to the selected listitem’s value?

    Edit: Here’s an example from one of my apps…

    public class MyVM
    {
      public int MyObjectId { get; set; }
    
      public List<SelectListItem> MyObjectList
      {
        get
        {
          List<SelectListItem> list = (from o in MyObjects select new SelectListItem 
            { Value = o.ObjectId.ToString(), Text = o.ObjectName }).ToList();
          list.Insert(0, new SelectListItem 
            { Value = "0", Text = "[Select an object]" });
          return list;
        }
      }
    }
    
    <%: Html.DropDownListFor(m => m.MyObjectId, Model.MyObjectList)%>
    

    You might have noticed the LINQ query that populates the list. In this example, I have a list (MyObjects) that was already populated by AutoMapper. You could simply return a static list if you prefer.

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

Sidebar

Related Questions

I have a main page which uses a ViewModel I have created: public class
I have a ViewModel: public class Page { public int Id { get; set;
I have a view model like so that is created from my validator. public
I am trying to post back some data using a viewmodel i have created
I have created presentation model and I want to map it (with AutoMapper) into
I have a strongly typed view that is using a viewmodel I created. I
I have created a viewmodel class for my custom view template. Right now I
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up
I have created a custom dialog for Visual Studio Setup Project using the steps

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.