Rectangle 27 6

this.ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);

Then write the event handler like this:

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
  ComboBox comboBox = (ComboBox) sender;
  string selected = (string) comboBox.SelectedItem;
}

Make sure that in your Page_Load you write this before setting the combobox default value or you will end up with this always being the selected item:

if (Page.IsPostBack)
  return;

I did what u wrote but always I get the first item..

I edited my answer, you need to be aware that the Page_Load is executed before the SelectedIndexChanged, so anything you do with the combobox here will affect your result. Use the if statement in the top section of Page_Load.

thank u so much . now I can get the true selected item.

ComboBox1.SelectedItem;
comboBox.SelectedItem;

you are right devlin! editet now

asp.net - Dropdownlist selected value at Selectedindexchanged event - ...

asp.net drop-down-menu selectedindexchanged
Rectangle 27 5

protected void list_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;
        string value = (string)list.SelectedValue;
    }

asp.net - Dropdownlist selected value at Selectedindexchanged event - ...

asp.net drop-down-menu selectedindexchanged
Rectangle 27 92

You should use view models and forget about ViewBag Think of it as if it didn't exist. You will see how easier things will become. So define a view model:

public class MyViewModel
{
    public int SelectedCategoryId { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; } 
}

and then populate this view model from the controller:

public ActionResult NewsEdit(int ID, dms_New dsn)
{
    var dsn = (from a in dc.dms_News where a.NewsID == ID select a).FirstOrDefault();
    var categories = (from b in dc.dms_NewsCategories select b).ToList();

    var model = new MyViewModel
    {
        SelectedCategoryId = dsn.NewsCategoriesID,
        Categories = categories.Select(x => new SelectListItem
        {
            Value = x.NewsCategoriesID.ToString(),
            Text = x.NewsCategoriesName
        })
    };
    return View(model);
}

and finally in your view use the strongly typed DropDownListFor helper:

@model MyViewModel

@Html.DropDownListFor(
    x => x.SelectedCategoryId,
    Model.Categories
)

Nothing wrong with using ViewBag to contain the SelectList - the MVC scaffolding mechanism does that. His problem was using the same name for the model and the selectlist.

@Liam, you call something that uses ViewBag an actual solution? I wouldn't :-) Cheers anyway.

@Darin Dimitrov: It's their for a reason! You call cluttering up your model with SelectListItems an actual solution?! It's also less efficient to pass objects back and forwards in this way. It needs serialising and it takes up bandwidth on the wire. Are you going to use this data when you post the form? No, don't put it in the Model then.

@Liam, I take your point as well, I just don't agree! :-)

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 91

You should use view models and forget about ViewBag Think of it as if it didn't exist. You will see how easier things will become. So define a view model:

public class MyViewModel
{
    public int SelectedCategoryId { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; } 
}

and then populate this view model from the controller:

public ActionResult NewsEdit(int ID, dms_New dsn)
{
    var dsn = (from a in dc.dms_News where a.NewsID == ID select a).FirstOrDefault();
    var categories = (from b in dc.dms_NewsCategories select b).ToList();

    var model = new MyViewModel
    {
        SelectedCategoryId = dsn.NewsCategoriesID,
        Categories = categories.Select(x => new SelectListItem
        {
            Value = x.NewsCategoriesID.ToString(),
            Text = x.NewsCategoriesName
        })
    };
    return View(model);
}

and finally in your view use the strongly typed DropDownListFor helper:

@model MyViewModel

@Html.DropDownListFor(
    x => x.SelectedCategoryId,
    Model.Categories
)

Nothing wrong with using ViewBag to contain the SelectList - the MVC scaffolding mechanism does that. His problem was using the same name for the model and the selectlist.

@Liam, you call something that uses ViewBag an actual solution? I wouldn't :-) Cheers anyway.

@Darin Dimitrov: It's their for a reason! You call cluttering up your model with SelectListItems an actual solution?! It's also less efficient to pass objects back and forwards in this way. It needs serialising and it takes up bandwidth on the wire. Are you going to use this data when you post the form? No, don't put it in the Model then.

@Liam, I take your point as well, I just don't agree! :-)

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 18

just in case someone comes with this question, this is how I do it, please forget about the repository object, I'm using the Repository Pattern, you can use your object context to retrieve the entities. And also don't pay attention to my entity names, my entity type Action has nothing to do with an MVC Action.

ViewBag.ActionStatusId = new SelectList(repository.GetAll<ActionStatus>(), "ActionStatusId", "Name", myAction.ActionStatusId);

Pay attention that the last variable of the SelectList constructor is the selected value (object selectedValue)

<div class="editor-label">
   @Html.LabelFor(model => model.ActionStatusId, "ActionStatus")
</div>
<div class="editor-field">
   @Html.DropDownList("ActionStatusId")
   @Html.ValidationMessageFor(model => model.ActionStatusId)
</div>

you sir are a genius!

Thanks Ben. Honestly I think that my solution is the simplest one of these available. Just create the SelectList and at the same time pass the selected value in the constructor of SelectList. Glad it helped you :)

This worked great for me when I found myself in a situation where I had a list of my viewmodel type and needed to do this problem multiple times on the same page in a loop. Much cleaner I think than having to re-tool my view models.

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 18

just in case someone comes with this question, this is how I do it, please forget about the repository object, I'm using the Repository Pattern, you can use your object context to retrieve the entities. And also don't pay attention to my entity names, my entity type Action has nothing to do with an MVC Action.

ViewBag.ActionStatusId = new SelectList(repository.GetAll<ActionStatus>(), "ActionStatusId", "Name", myAction.ActionStatusId);

Pay attention that the last variable of the SelectList constructor is the selected value (object selectedValue)

<div class="editor-label">
   @Html.LabelFor(model => model.ActionStatusId, "ActionStatus")
</div>
<div class="editor-field">
   @Html.DropDownList("ActionStatusId")
   @Html.ValidationMessageFor(model => model.ActionStatusId)
</div>

you sir are a genius!

Thanks Ben. Honestly I think that my solution is the simplest one of these available. Just create the SelectList and at the same time pass the selected value in the constructor of SelectList. Glad it helped you :)

This worked great for me when I found myself in a situation where I had a list of my viewmodel type and needed to do this problem multiple times on the same page in a loop. Much cleaner I think than having to re-tool my view models.

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 4

public string type { get; set; }

    public List<SelectListItem> DropdownList { 
        get {
                return new List<SelectListItem> 
                {
                    new SelectListItem { Selected = false,Value = "pdf", Text = "Pdf" },
                    new SelectListItem { Selected = false,Value = "excel", Text = "Excel" },
                    new SelectListItem { Selected = false,Value = "word", Text = "Word" }
                };
            }
    }
}`
public  ActionResult Test(StaticList list)
    {
        list.type = "pdf";
        return View(list);
    }
@Html.DropDownListFor(model => model.type, Model.DropdownList)

Thanks for answer. I have applied the prior solution, but I marked as helpful your answer. Regards...

asp.net mvc - Could not pass Dropdownlist Selected Value to Action Met...

asp.net-mvc razor parameter-passing html.dropdownlistfor selectlist
Rectangle 27 8

Well its very simple in controller you have somthing like this:

ViewBag.Profile_Id = new SelectList(db.Profiles, "Id", "Name", model.Profile_Id);
@Html.DropDownList("Profile_Id")

--View (Option B) --> Send a null value to the list

@Html.DropDownList("Profile_Id", null, "-- Choose --", new {  @class = "input-large" })

Can please you explain what is the difference between using null instead of ViewBag.Profile_Id as SelectList

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 8

Well its very simple in controller you have somthing like this:

ViewBag.Profile_Id = new SelectList(db.Profiles, "Id", "Name", model.Profile_Id);
@Html.DropDownList("Profile_Id")

--View (Option B) --> Send a null value to the list

@Html.DropDownList("Profile_Id", null, "-- Choose --", new {  @class = "input-large" })

Can please you explain what is the difference between using null instead of ViewBag.Profile_Id as SelectList

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 5

Replace below line with new updated working code:

@Html.DropDownList("NewsCategoriesID", (SelectList)ViewBag.NewsCategoriesID)
@Html.DropDownListFor(model => model.NewsCategoriesID, ViewBag.NewsCategoriesID as List<SelectListItem>, new {name = "NewsCategoriesID", id = "NewsCategoriesID" })

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 5

I want to put the correct answer in here, just in case others are having this problem like I was. If you hate the ViewBag, fine don't use it, but the real problem with the code in the question is that the same name is being used for both the model property and the selectlist as was pointed out by @RickAndMSFT

Simply changing the name of the DropDownList control should resolve the issue, like so:

@Html.DropDownList("NewsCategoriesSelection", (SelectList)ViewBag.NewsCategoriesID)

It doesn't really have anything to do with using the ViewBag or not using the ViewBag as you can have a name collision with the control regardless.

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 5

Replace below line with new updated working code:

@Html.DropDownList("NewsCategoriesID", (SelectList)ViewBag.NewsCategoriesID)
@Html.DropDownListFor(model => model.NewsCategoriesID, ViewBag.NewsCategoriesID as List<SelectListItem>, new {name = "NewsCategoriesID", id = "NewsCategoriesID" })

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 5

I want to put the correct answer in here, just in case others are having this problem like I was. If you hate the ViewBag, fine don't use it, but the real problem with the code in the question is that the same name is being used for both the model property and the selectlist as was pointed out by @RickAndMSFT

Simply changing the name of the DropDownList control should resolve the issue, like so:

@Html.DropDownList("NewsCategoriesSelection", (SelectList)ViewBag.NewsCategoriesID)

It doesn't really have anything to do with using the ViewBag or not using the ViewBag as you can have a name collision with the control regardless.

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 2

Name you dropdown list must be equals parameter name in your Action.

Edit: I repeat you code, all work fine if RenderReport have string SelectedValue parameter

public ActionResult RenderReport(string SelectedValue, string name, string fileName, string dataSource, string table, string filter)
{
    //Codes for rendering report
    ...
}
@Html.DropDownList("type", Model.Values, "-- select an option --", new {id = "type"})

I had also tried with the same value before asking the question. But did not worked. Any idea?

asp.net mvc - Could not pass Dropdownlist Selected Value to Action Met...

asp.net-mvc razor parameter-passing html.dropdownlistfor selectlist
Rectangle 27 2

I'm almost embarrassed to say that on the page there was a Hidden Field for AttendeeId, which was being assigned the correct value. This meant that there were two values for AttendeeId being posted back to the server, but the Model binder used the second, Empty value to bind to the view model.

So the takeaway for me, is always inspect the POST as well as what's being bound to the model....

DropDownList selected value not posted to server ASP.NET MVC - Stack O...

asp.net-mvc
Rectangle 27 4

I drilled down the formation of the drop down list instead of using @Html.DropDownList(). This is useful if you have to set the value of the dropdown list at runtime in razor instead of controller:

<select id="NewsCategoriesID" name="NewsCategoriesID">
    @foreach (SelectListItem option in ViewBag.NewsCategoriesID)
    {
        <option value="@option.Value" @(option.Value == ViewBag.ValueToSet ? "selected='selected'" : "")>@option.Text</option>

    }
</select>

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 4

I drilled down the formation of the drop down list instead of using @Html.DropDownList(). This is useful if you have to set the value of the dropdown list at runtime in razor instead of controller:

<select id="NewsCategoriesID" name="NewsCategoriesID">
    @foreach (SelectListItem option in ViewBag.NewsCategoriesID)
    {
        <option value="@option.Value" @(option.Value == ViewBag.ValueToSet ? "selected='selected'" : "")>@option.Text</option>

    }
</select>

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 3

If you want to use the SelectList, then I think this bug report might assist - http://aspnet.codeplex.com/workitem/4932

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected
Rectangle 27 3

If you want to use the SelectList, then I think this bug report might assist - http://aspnet.codeplex.com/workitem/4932

asp.net mvc 3 - dropdownlist set selected value in MVC3 Razor - Stack ...

asp.net-mvc-3 drop-down-menu razor selected