2

Controller

var productList = Enumerable.Range(1, 80).Select(
    x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }
);
ViewData["products"] = new SelectList(productList.ToList(), "Value", "Text");

View

<%: Html.DropDownList("products", ViewData["products"] as SelectList, "--select--")%>
<%: Html.ValidationMessage("products", "Please Select the Product from the List")%> 
//This doesnt works on (ModelState.IsValid) I know the dropdown list data is coming
//from the view data not model , thats why model doesnt validate the particular dropdown 
//list while it validates other fields which are linked to model,
//Just want to know, how can i validate the above dropdownlist 

1 Answer 1

11

You are binding both the name of the ddl and the values to products. That's wrong. Of course that's the least problem with your code. The far bigger and more serious problem is that you are using ViewData instead of using strongly typed views and view models.

So there are two possibilities:

  1. The crappy one: have a property on your view model to which you will bind your dropdown value.

    [Required(ErrorMessage = "Please Select the Product from the List")]
    public string SelectedProduct { get; set; }
    

    and then use this property name as first argument to the weakly typed DropDownList helper and the ViewData as second agrument:

    <%= Html.DropDownList( 
        "SelectedProduct", 
        ViewData["products"] as SelectList, 
        "--select--"
    ) %>
    <%= Html.ValidationMessage("SelectedProduct") %>
    
  2. The correct way: which of course is using real view models (I am sick of repeating it, just google, you will get like gazillions of answers, just by me, and just on this site on this topic). It will look like this:

    <%: Html.DropDownListFor( 
        x => x.SelectedProduct, 
        Model.Products, 
        "--select--"
    ) %>
    <%= Html.ValidationMessageFor(x => x.SelectedProduct) %>
    
Sign up to request clarification or add additional context in comments.

4 Comments

Yeh i know i shud have been using the view models , i am using model for all the other fields , but this product is from different table and the values are hardcoded , therefore I am passing through view data and then saving it to the other table.
@Mr A, why are you talking about tables? This is completely unrelated where the data comes from: tables, XML files, other web services, facebook account, ... It doesn't really matter. That's why you have a view model => in order to aggregate all the data that is required by your views. So simply use a mapping layer to convert your domain objects into one single and clean view model and do the things the right way.
Yeh I have seen the correct way of doing it , nd this is the 10th or 11th time you have told me to use view models , but the problem is I am in the middle of the website completion nd i need to publish it live as soon as possible , Once this is done , I will refactor the whole website using the correct way .. I was given the website at time , when i knew nothing about mvc
+1 @Darin Dimitrov - for using gazillions and for repeating for the gazillionth time your sage advice about using view models

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.