Model binding works by binding to the value of your model property. You need to set the value of property SagId
in the controller before you pass the model to the view.
Your code in your controller method should be something like
var model = new YourModel()
{
SagId = 7
};
ViewBag.SagId = new SelectList(db.Sags, "Id", "Emne");
return View(model);
Note that there is not point setting the 4th parameter in the SelectList
constructor. It's ignored when binding to a model property because internally the DropDownListFor()
method builds its own IEnumerable<SelectListItem>
and sets the Selected
property based on the value of the property your binding to.
Note also that you should not use the same names for the property your binding to (refer Can the ViewBag name be the same as the Model property name in a DropDownList?), and I strongly recommend that you use a view model, especially when editing, and that view model will contain a property public IEnumerable<SelectListItem> SagList { get; set; }
and in the view it will be @Html.DropDownListFor(m => m.SagId, Model.SagList, new { ... })