asp.net mvc - How to set default value to select list in MVC during run time -
i have view in loop through model , display details in editable mode. 1 of model value select list below
@if (model != null) { (int = 0; < 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 viewbag.activity_code_type
contain values internal & standard
when submitting if user selected internal
value 1
pass controller , if standard
2
, here default value "--- select activity code type ---"
now when open same request in edit mode if model value provider_service_dtls[i].activity_code_type
1
select list should default select internal
, standard
if 2
.
i coded 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 not working expected giving result below picture
here should default selected internal
. change achieve 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 loop coded this
@html.editorfor(m => m.provider_service_dtls, new { options = (selectlist)@viewbag.activity_code_type })
i getting error
'system.web.mvc.htmlhelper' not contain definition 'editorfor' , best extension method overload 'system.web.mvc.html.editorextensions.editorfor(system.web.mvc.htmlhelper, system.linq.expressions.expression>, string, object)' has invalid arguments
unfortunately @html.dropdownlistfor()
behaves little differently other helpers when rendering controls in loop. has been reported issue on codeplex (not sure if bug or limitation)
create custom editortemplate
type in collection.
in /views/shared/editortemplates/provider_service_dtls.cshtml
(note name must match name of 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 other properties of provider_service_dtls
and in main view, pass selectlist editortemplate additionalviewdata
@model yourassembly.provider_preapproval @using (html.beginform()) { .... // html helpers 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>
, generate controls each item in collection
edit
an alternative create new selectlist
in each iteration of for
loop, need set selected
property of selectlist
. means viewbag
property must ienumerable<t>
, not selectlist
, example in controller
viewbag.activitycodetypelist = new[] { new { id = 1, name = "internal" }, new { id = 2, name = "standard" } }
and in view
for (int = 0; < 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 ---") }
Comments
Post a Comment