@Html.Dropdownlistfor How to Set Default Value

@Html.DropDownListFor how to set default value

Like this:

@Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
{ new SelectListItem{Text="Active", Value="True"},
new SelectListItem{Text="Deactive", Value="False"}},"Select One")

If you want Active to be selected by default then use Selected property of SelectListItem:

@Html.DropDownListFor(model => model.Status, new List<SelectListItem> 
{ new SelectListItem{Text="Active", Value="True",Selected=true},
new SelectListItem{Text="Deactive", Value="False"}},"Select One")

If using SelectList, then you have to use this overload and specify SelectListItem Value property which you want to set selected:

@Html.DropDownListFor(model => model.title, 
new SelectList(new List<SelectListItem>
{
new SelectListItem { Text = "Active" , Value = "True"},
new SelectListItem { Text = "InActive", Value = "False" }
},
"Value", // property to be set as Value of dropdown item
"Text", // property to be used as text of dropdown item
"True"), // value that should be set selected of dropdown
new { @class = "form-control" })

Setting default value to Html.DropDownListFor

If you want to pass default value for the dropdown list than you can do is pass default viewmodel/model with default value

Example :

public ActionResult New()
{
MyViewModel myViewModel = new MyViewModel ();
myViewModel.OfficeAddressCountry = default value;
//Pass it to the view using the `ActionResult`
return ActionResult( myViewModel);
}

or you can try this

 var list = new SelectList(Db.Countries, "CountryCode", "CountryName")
list.Add(new SelectListItem(){Value = 1,Text = "Deafult Item",Selected = true };)
ViewBag.ListOfCountries = list;

How to set dynamically default value for DropDownListFor using asp.net mvc4

If foreach loop @Html.DropDownListFor ,@Html.HidenFor, @Html.TextBoxFor or any other input element never working,
because in razor input/select/textarea create a unique html name/id attribute. But using foreach it can't do this.
so use for loop or EditorTemplates rather than foreach.

Other wise you can generate the html but you can't send list of item in your action.

Example:

Model:

public class UserEditorViewModel
{
public string UserId { get; set; }
public string RoleId { get; set; }
public string UserName { get; set; }
public IEnumerable<Roles> Roles { get; set; }
}

EditorTemplates need to exist in either Views/Shared/EditorTemplates or Views/ControllerName/EditorTemplates and name of the view (by default) should be the name of the object(Or Name of the Model) you want to use it as the template.

Editortemplates:

Views/Shared/EditorTemplates/UserEditorViewModel.cshtml

@model UserEditorViewModel

<div class="form-group">
@Html.DisplayNameFor(m => m.UserId)
@Html.EditorFor(m => m.UserId)
</div>
<div class="form-group">
@Html.DisplayNameFor(m => m.UserName)
@Html.EditorFor(m => m.UserName)
</div>
<div class="form-group">
@Html.DisplayNameFor(m => m.RoleId)
@Html.DropDownListFor(m => m.RoleId, new SelectList(Model.Roles,"RoleId","RoleName",Model.RoleId))
</div>

View :

@model UserEditorViewModel

@using (Html.BeginForm("--Your Action--", "--Your Controller--"))//Or use Ajax.BeginForm if you need
{
@Html.EditorForModel()
<input type="submit" value="Save" />
}

@Html.DropDownList how to set default value be ACTIVE

Use the third Parameter as a default value.

@Html.DropDownList("Status", new List<SelectListItem> { new SelectListItem { Text="Active", Value = "True",Selected=true  }, Model.SomeDefaultValue, "Select The Status..")

The Good Way to Create a ViewModel and Include all the data to that viewModel and render it like this.

@Html.DropDownList("Status", Model.ListOfSomeThing, Model.SomeDefaultValue, "Select The Status..") 

(This would be more Cleaner.)

I hope it helps!

Example:

ViewModel

 public class SimpleViewModel{
public List<SelectListItem> Status_List {get; set}
public string Default_Status {get; set}

public SimpleViewModel(){
CustomerList = new List<SelectListItem>();
Default_Status= "Active"; //default value.
}

Action Method

public ActionResult Index()
{
SimpleViewModel viewModel = new SimpleViewModel();
viewModel.Status_List = // Initialize it here.
viewModel.Default_Status = // If you want to Change you Default_Status.

return view(viewModel);
}

View

   @Html.DropDownList("Customer", Model.CustomerList, Model.Customer)

Choose Default Value from DropDownList

You can add another field to your ViewModel say SelectedStatus and set its value to your desired default. Then you can use @Html.DropDownListFor to display default value.

public class ViewModel
{
public Guid SelectedStatus {get;set;}
public List<ListStatus> Statuses { get; set; }
}

In your controller

[HttpGet]
public ActionResult Index()
{
List<ListStatus> lstStatus = new List<ListStatus>(){
new ListStatus() { Name="X",StatusID = Guid.NewGuid() },
new ListStatus() { Name="Y",StatusID = Guid.NewGuid() },
new ListStatus() { Name="Z",StatusID = Guid.NewGuid() }
};

ViewModel objModel = new ViewModel();
objModel.Statuses = lstStatus;
objModel.SelectedStatus = lstStatus[1].StatusID; // Select whatever you want here to be default.

return View(objModel);
}

In your view

@Html.DropDownListFor(m=>m.SelectedStatus, Model.Statuses.Select(s => new SelectListItem() { Text = s.Name, Value = s.StatusID.ToString() }), new { @class = "medium", required = true })

Here is fiddle : https://dotnetfiddle.net/vTJhaj

Setting default value of a Html.DropDownList()

When constructing your SelectListItem classes, set the Selected property to true for the item you want initially selected.



Related Topics



Leave a reply



Submit