Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.DropDownListFor set selected value

I create a @Html.DropDownListFor and populate it from the database. How can I set a selected value to the drop down?

My View:

@Html.DropDownListFor(m => m.Forms, new SelectList(Model.Forms, "FormsCreatorID", "FormName"),
            "Select a Form", new { @class = "form-control" })

My Controller:

var forms = db.formscreators.Where(fp => fp.PropertyID == id || fp.PropertyID == 0)
            .OrderByDescending(x => x.PropertyID).GroupBy(x => x.FormName).Select(x => x.FirstOrDefault()).ToList();
var viewModel = new ListFormsCreator { Forms = forms };

My ViewModel:

public class ListFormsCreator
{
    public List<formscreator> Forms { get; set; }
}

My Database Model:

public partial class formscreator
{
    public int FormsCreatorID { get; set; }
    public string FormName { get; set; }
    public int PropertyID { get; set; }
}

Thanks

like image 278
user6824563 Avatar asked Jul 21 '26 00:07

user6824563


1 Answers

You should add another property to your view model for the store/pass the selected option.

public class ListFormsCreator
{
    public int SelectedFormId  { set;get;}
    public List<formscreator> Forms { get; set; }
}

Now in your GET action, you can set that value

var viewModel = new ListFormsCreator() { Forms = forms };
viewModel.SelectedFormId  = 2 ; // This will select the option with 2 as FormsCreatorID
return View(viewModel);

And in the view use the lambda expression with that property as the first parameter of the DropDownListFor helper method.

@model ListFormsCreator

@Html.DropDownListFor(m => m.SelectedFormId  , 
                         new SelectList(Model.Forms, "FormsCreatorID", "FormName"),
                        "Select a Form", new { @class = "form-control" })

The DropDownListFor helper method will use the value of SelectedFormId property and select the option which has the same value attribute value from the list of options of that SELECT element.

You can also remove the dependency on formscreator class from the view model, by replacing it with a list of SelectListItem

public class ListFormsCreator
{
    public int SelectedFormId  { set;get;}
    public List<SelectListItem> Forms { get; set; }
}

Now in your GET action, you can use the Select method to generate the lsit of SelectListItem from your other collection.

var viewModel = new ListFormsCreator();
viewModel.Forms = someCollection.Select(a=>new SelectListItem { 
                                           Value=a.FormsCreatorId.ToString(), 
                                           Text=a.FormName})
                 .ToList();
viewModel.SelectedFormId  = 2 ; // This will select the option with 2 as FormsCreatorID
return View(viewModel);

Assuming someCollection is a collection of formscreator objects Now in the view code is much simpler

@Html.DropDownListFor(m => m.SelectedFormId, Model.Forms ,"Select a Form")
like image 106
Shyju Avatar answered Jul 22 '26 14:07

Shyju