Persist Values for DropDownList Editor Templates
-
11-10-2019 - |
Question
I have this on my view and this works well, persisting the country selected:
<%: Html.DropDownListFor(model => model.CountryId, Model.CountryList ,"Select Country") %>
However when I create an editor template for this, the country selected is not persisted
So, I change my current view to this:
<%: Html.EditorFor(model => model.CountryId,new { countries = Model.CountryList}) %>
Then I create my editor template like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Int64?>" %>
<%= Html.DropDownList(
String.Empty /* */,
(SelectList)ViewData["countries"],
Model
)
%>
Solution
I would try something like this:
Code in parent view:
<% Html.RenderPartial("YourPartialView", Model) %>
Code in editor view:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ModelFromParent>" %>
<%= Html.DropDownList(
String.Empty /* */,
new SelectList(Model.Countries,"Id", "Title"),
"Select Country"
)
%>
I didn't compile this. But you get the general idea.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow