1

I have the following, which posts "IT" and "HR" as values in model. I want IT and HR as Label, and 0 and 1 as values to controller.

@Html.DropDownList("DepartmentId", new SelectList(new[] { "IT", "HR" }))

And when i get the view, how can i have pre-selected value in the dropdown with the matching value in Department ID in model. So if an item has Id as HR, the dropdown should already have HR as selected, and then a person can change it to IT, and that option can be saved.

Any Help ?? Please note i don't want to change my model.

ANSWER Big Thanks to Terry Nederveld for giving this answer :

 @Html.DropDownList("DepartmentId", new SelectList(new[] { new KeyValuePair<string, int>("IT", 1), new KeyValuePair<string, int>("HR", 2) }, "Value", "Key", (Model.ServiceItem.DepartmentId.Equals(1) ? 1 : 2)))
2
  • Create a model Department with variables DepartmentText, DepartmentValue, and fill the data to a list Commented May 15, 2012 at 15:58
  • I don't want to create a new model here for Department. Commented May 15, 2012 at 16:02

3 Answers 3

3

Assuming you're using MVC3/4, the easiest way is to add a 'Departments' property to your model. Then you can do this in your view:

@Html.DropDownListFor(m => m.Department.DepartmentId, Model.Departments) 

With your model looking like this:

public class ViewModel
{
    public List<SelectListItem> Departments
    {
        get;
        private set;
    }

    public Department Department { get; set; }


    public ViewModel()
    {
        Departments = new List<SelectListItem>();
        //Assume you'll be getting the list from db here?
        Departments .Add(new SelectListItem { Text = "HR", Value = "IdValueHere" });
        Departments .Add(new SelectListItem { Text = "IT", Value = "IdValueHere" });
    }
}

This takes care of selecting the correct item.

Sign up to request clarification or add additional context in comments.

7 Comments

For some reasons, I don't want to change model, i want to do it in view only.
I'd recommend you overcome those reasons, as they appear to be fighting against the framework ;)
:) Sometimes you have to do it the other way. !!
Are the reasons that you're using a domain object as the model and you don't want to clutter it with UI specific properties?
This is the point of View Models though. You create a wrapper object for your 'proper' object that contains various properties specifically for UI framework purposes. The main model object is left untouched.
|
2

David Masters approach is the one that I use normally. But since you don't want to change the Model. Here is one approach that will work:

@Html.DropDownList("Departments", new SelectList(new [] { new KeyValuePair<string, int>( "IT", 0 ), new KeyValuePair<string, int>( "HR", 1 )},"Value", "Key")) 

10 Comments

That is one step closer to my goal. I am able to post the correct values, how can i actually set the selected value in this drop down, lets say i have a property in Model with name deptid which will contain either IT or HR, , can i use this property to set the selected value of the above dropdown ?? :) THanks !!
@Html.DropDownList( "Departments", new SelectList( new[] { new KeyValuePair<string, int>( "IT", 0 ), new KeyValuePair<string, int>( "HR", 1 ) }, "Value", "Key", Model.deptid ) ) Should do it for you.
I read it wrong, so you are trying to select the proper item with the key not the value? If the DeptId is an integer of 0/1 then the above will work. Otherwise you will need to change it to us the following (Model.deptid.Equals("HR") ? 1 : 0) instead of the Model.deptid directly.
The above did work, but now i get three values in dropdown, the (HR)selected value, IT and HR !! But there should be only two values, with HR as selected, right ??
Just checking in, is it working now for you using the (Model.deptid.Equals...)?
|
0

See the response from eudaimos (about 50% of way down replies) here for a solution

2 Comments

apologies - I thought this was how you were supposed to link answers, otherwise how else can the question ever get marked as answered ? I certainly didn't intent to cause an offence - apologies again.
No offence caused, just my personal opinion; I'd leave links against the questions comments, otherwise your just taking the credit for someone else's answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.