保留 DropDownList 编辑器模板的值

发布于 2024-10-08 12:10:54 字数 613 浏览 0 评论 0原文

我的视图中有这个,并且效果很好,保留了所选的国家/地区:

<%: Html.DropDownListFor(model => model.CountryId, Model.CountryList ,"Select Country") %>

但是,当我为此创建编辑器模板时,所选的国家/地区不会保留

所以,我将当前视图更改为:

 <%: Html.EditorFor(model => model.CountryId,new { countries = Model.CountryList}) %>

然后我像这样创建编辑器模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Int64?>" %>
<%= Html.DropDownList(
        String.Empty /* */, 
        (SelectList)ViewData["countries"], 
        Model
    )
%>

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
    )
%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-10-15 12:10:55

我会尝试这样的事情:

父视图中的代码:

 <% Html.RenderPartial("YourPartialView", Model) %>

编辑器视图中的代码:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ModelFromParent>" %>
<%= Html.DropDownList(
    String.Empty /* */, 
    new SelectList(Model.Countries,"Id", "Title"), 
    "Select Country"
)

%>

我没有编译这个。但你已经了解了总体思路。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文