-1

I need to select the dropdown in html helper. I pass my data in IEnumerable using ViewData please some one help me how to select it with value.

My Code is

@Html.DropDownList("Brand", ViewData["Brand"] as IEnumerable<SelectListItem>, item.BrandMaster, new { @class = "form-control Repeat TableMust"})

Here

  1. ViewData["Brand"] is my Dropdown list.
  2. I want to select item.BrandMaster value in dropdown.

But the problem is it's showing the text but it does not contain the value. The value is "" empty, how to select it in html helpers?

6
  • This should do @Html.DropDownList("Brand", new SelectList((System.Collections.IEnumerable) ViewData["Brand"], "Id", "Name")) Commented Nov 16, 2014 at 9:28
  • what is id and what is name Commented Nov 16, 2014 at 9:33
  • id could be your value and Name could be your Text. Say Brand table has BrandID and BrandName the ID =BrandID and Text = BrandName Commented Nov 16, 2014 at 9:36
  • @Vivekh, This does not bind to property BrandMaster Commented Nov 16, 2014 at 9:46
  • Yes, its not working Commented Nov 16, 2014 at 9:48

3 Answers 3

1

If your ViewData["Brand"] contains an object IEnumerable<SelectListItem>, I guess you have already used its constructor or more likely Object Initializer, e.g.:

List<SelectListItem> brand = new List<SelectListItem>()
{
   new { Value = 1 , Text = "something 1"  },
   new { Value = 2 , Text = "something 2"  },
   new { Value = 3 , Text = "something 3"  },
};
ViewData["Brand"] = brand;

In your View, you use the list as a second parameter for DropDownList helper. You also need to provide what its Value and Text. The last parameter indicates which value is to be selected by default:

@Html.DopDownList("Brand",
    (List<SelectListItem>)ViewData["Brand"],
    "value", "text", 2)

You could also try SelectList container to "fill" the DropDownList helper. Simply send your list of Objects to view in ViewData etc., and use it as a second parameter. Then provide Value and Text as the third and the fourth parameters, respectively. The last parameter corresponds to the value of your selected item in the list.

@Html.DropDownList("Brand", new SelectList(
    Model.ListOfYourObjects,
    "<<< Property name as value in the Object, e.g. ID",
    "<<< Property name as text in the Object, e.g. Name/Title/etc.",
    <<< value of thecurrent ID >>>));

e.g.

public class City
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Then

ViewData["Cities"] = dbContext.Cities.ToList();
//or
ViewData["Cities"] = new List<City>(); // fill this list

In a view:

@model List<City>
// ...
@Html.DropDownList(
    "Brand",
    new SelectList(
        Model, "ID", "Name",
        <<< value of ID to be selected or null>>>
    )
);

ID and Name are property names in the class City which is important here in order for SelectList to work.

I couldn't try this code but this should give you something to work with. Hope it can help.

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

Comments

0

If the property your binding to is named BrandMaster then it should be

@Html.DropDownListFor(m => m.BrandMaster, ViewData["Brand"] as IEnumerable<SelectListItem>, new { @class = "form-control Repeat TableMust"})

If the value of BrandMaster matches the value of one of the SelectList items, then it will be selected by default.

Always use the strongly types helpers!

Edit

To use create a dropdown for the property of a collection, you need to use a custom EditorTemplate for your model

public class MyModel
{
  public int BrandMaster { get; set; }
  public string AnotherProperty { get; set; }
}

MyModel.cshtml (in Views/Shared/EditorTemplates

@model MyModel
@Html.DropDownListFor(m => m.BrandMaster, ViewData["Brand"] as IEnumerable<SelectListItem>, new { @class = "form-control Repeat TableMust"})
@Html.TextBoxFor(m => m.AnotherProperty)

Main View

@model IEnumerable<MyModel>
@using (Html.BeginForm())
{
  @Html.EditorFor(m => m, new { Brand = ViewData["Brand"])
  ....
}

6 Comments

No i don't want to select default value i want to select item.BrandMaster Value
That what it will do! Read my answer
if it is IEnumberable<model> then how to select it
Then you need to use a custom EditorTemplate. I'll update answer shortly (you would never have been able to post back your collection with the code in your question anyway)
@Dinesh, Refer also this answer for a more detailed explanation on using DropDownListFor() in a collection
|
0

When you are building your dropdown list you can select the selected item using Selected property of SelectListItem

myModelCollection = SomeService.GetModels();
var dropdown = myModelCollection.Select(s => new SelectListItem {Value = s.Id, Name = s.Name, Selected = s.Id == YourSelectedValue} ).ToList();

Comments

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.