Asked  7 Months ago    Answers:  5   Viewed   41 times

As the question says: How to set selectedValue in DropDownListFor Html helper?

Tried most of the other solutions but none worked that's why I am opening a new question.

Nothing of these helped:

@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2), htmlAttributes: new { @class = "form-control" })

//Not working with or without cast
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", (ProjectName.Models.TipDepozita)Model.TipoviDepozita.Single(x => x.Id == 2)), htmlAttributes: new { @class = "form-control" })
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", (ProjectName.Models.TipDepozita)Model.TipoviDepozita.Where(x => x.Id == 2).FirstOrDefault()), htmlAttributes: new { @class = "form-control" })

@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", new SelectListItem() { Value="2", Selected=true}), htmlAttributes: new { @class = "form-control" })

I would like to avoid manual creation of SelectListItems or a ViewModel just for the list if possible.

 Answers

84

When you use the DropDownListFor() (or DropDownList()) method to bind to a model property, its the value of the property that sets the selected option.

Internally, the methods generate their own IEnumerable<SelectListItem> and set the Selected property based on the value of the property, and therefore setting the Selected property in your code is ignored. The only time its respected is when you do not bind to a model property, for example using

@Html.DropDownList("NotAModelProperty", new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2))

Note your can inspect the source code, in particular the SelectInternal() and GetSelectListWithDefaultValue() methods to see how it works in detail.

To display the selected option when the view is first rendered, set the value of the property in the GET method before you pass the model to the view

I also recommend your view model contains a property IEnumerable<SelectListItem> TipoviDepozita and that you generate the SelectList in the controller

var model = new YourModel()
{
    TipoviDepozita = new SelectList(yourCollection, "Id", "Naziv"),
    TipPopustaId = 2 // set the selected option
}
return View(model);

so the view becomes

@Html.DropDownListFor(m => m.TipPopustaId, Model.TipoviDepozita, new { @class = "form-control" })
Tuesday, June 1, 2021
 
Bere
answered 7 Months ago
67

Use the audio element's volume property. From W3:

The element's effective media volume is volume, interpreted relative to the range 0.0 to 1.0, with 0.0 being silent, and 1.0 being the loudest setting, values in between increasing in loudness. The range need not be linear. The loudest setting may be lower than the system's loudest possible setting; for example the user could have set a maximum volume.

Ex: sounds[id].volume=.5;

Thursday, June 24, 2021
 
nighter
answered 6 Months ago
39

args was generated by a filter, and is a RouteValueDictionary

That's the key point here. In this case make sure you are using the correct overload of the BeginForm method:

@using(Html.BeginForm(
    "Profile", 
    "Account",   
    args, 
    FormMethod.Post, 
    new RouteValueDictionary(new { @class = "mainForm" })
))
{
    ...
}

Notice the last argument? It must be an IDictionary<string, object> for this to work.

In your example it is this overload that gets picked up. But since you are passing a RouteValueDictionary for the routeValues parameter instead of an anonymous object it gets messed up.

So, you should either have both routeValues and htmlAttributes as dictionaries or both as anonymous objects.

Saturday, August 14, 2021
 
rlanvin
answered 4 Months ago
18

See resolved solution here on ASP.NET Forum

Friday, August 27, 2021
 
peixotorms
answered 4 Months ago
95

I dont think that using EditorFor you can set a default value. Consider setting it in the accessors on your model?

To do it on other types ( TextBoxFor etc ) You can set a value but not a default value. So you would need to do:

@if(Model.something == null)
{
   @Html.TextBoxFor(m => m.ID, new { @Value = "Value!"})
} else {
   @Html.TextBoxFor(m => m.ID)
}

As I would reccomend:

private DateTime? _date;
public DateTime? date {
get {
   if(_date == null)
      _date = DateTime.Now;
   return _date;
}
set {
   _date = value;
}
}

Using things such as jquery date picker allow you to have a default value if the problem is that you are just posting nothing back if it has not been selected.

Saturday, August 28, 2021
 
Sen
answered 3 Months ago
Sen
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share