How to set default value to select list in MVC during run time
I have view in which it loop through the model and display the details in editable mode. One of the model value is from a select list like below
@if (Model != null)
{
for (int i = 0; i < Model.provider_service_dtls.Count; i++)
{
<tr>
<td> @Html.DropDownListFor(m => m.provider_service_dtls[i].activity_code_type,
(SelectList)@ViewBag.activity_code_type, "--- Select Activity Code Type ---", new { @class = "m-wrap" })</td>
<td>@Html.TextBoxFor(m => m.provider_service_dtls[i].activity_name)</td>
</tr>
}
}
Here the ViewBag.activity_code_type
contain the values Internal & Standard
when submitting if user selected Internal
its value 1
will pass to controller and if Standard
it will be 2
and here the default value will be "--- Select Activity Code Type ---"
Now when i open the same request in edit mode if the model value for provider_service_dtls[i].activity_code_type
is 1
the select list should be default select as Internal
and Standard
if it is 2
.
I coded like this
@Html.DropDownListFor(m => m.provider_service_dtls[i].activity_code_type,
(SelectList)@ViewBag.activity_code_type, Model.provider_service_dtls[i].activity_code_type)
But it is not working as expected it is giving the result as below picture
Here it should default selected Internal
. What is the change to do achieve the same?
Edited
Model
public partial class provider_service_dtls
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long service_id { get; set; }
public long preapproval_id { get; set; }
public string activity_code_type { get; set; }
public string activity_type { get; set; }
public string activity_type_name { get; set; }
public string activity_code { get; set; }
public string activity_name { get; set; }
public string internal_activity_code { get; set; }
public string internal_activity_name { get; set; }
[ForeignKey("preapproval_id"), InverseProperty("provider_service_dtls")]
public virtual provider_preapproval preapproval { get; set; }
}
Editor template
@model Provider.Models.provider_preapproval
@Html.DropDownListFor(m => m.activity_code_type, (SelectList)ViewData["options"])
View
Inside the for loop i coded like this
@Html.EditorFor(m => m.provider_service_dtls,
new { options = (SelectList)@ViewBag.activity_code_type })
I am getting an error
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'EditorFor' and the best extension method overload 'System.Web.Mvc.Html.EditorExtensions.EditorFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, string, object)' has some invalid arguments
Unfortunately @Html.DropDownListFor()
behaves a little differently than other helpers when rendering controls in a loop. This has been previously reported as an issue on CodePlex (not sure if its a bug or just a limitation)
Create a custom EditorTemplate
for the type in the collection.
In /Views/Shared/EditorTemplates/provider_service_dtls.cshtml
(note the name must match the name of the type)
@model yourAssembly.provider_service_dtls
@Html.HiddenFor(m => m.service_id)
@Html.DropDownListFor(m => m.activity_code_type, (SelectList)ViewData["options"], "--- Select Activity Code Type ---")
.... // html helpers for other properties of provider_service_dtls
and then in the main view, pass the SelectList to the EditorTemplate as additionalViewData
@model yourAssembly.provider_preapproval
@using (Html.BeginForm())
{
.... // html helpers for other properties of provider_preapproval
@Html.EditorFor(m => m.provider_service_dtls, new { options = (SelectList)@ViewBag.activity_code_type })
...
The EditorFor()
methods accepts IEnumerable<T>
and will generate the controls for each item in the collection
Edit
An alternative is to create a new SelectList
in each iteration of the for
loop, where you need to set the Selected
property of SelectList
. This means that your ViewBag
property must be IEnumerable<T>
, not a SelectList
, for example in the controller
ViewBag.ActivityCodeTypeList = new[]
{
new { ID = 1, Name = "Internal" },
new { ID = 2, Name = "Standard" }
}
and in the view
for (int i = 0; i < Model.provider_service_dtls.Count; i++)
{
@Html.DropDownListFor(m => m => m.provider_service_dtls[i].activity_code_type,
new SelectList(ViewBag.ActivityCodeTypeList, "ID", "Name", Model.provider_service_dtls[i].activity_code_type),
"--- Select Activity Code Type ---")
}