设置选择列表中选择默认值的编辑模板内 [英] Setting default selected value of selectlist inside an editor template

查看:104
本文介绍了设置选择列表中选择默认值的编辑模板内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code设置我的选择列表中的默认值:

 公众的ActionResult寄存器()
{
    IList的<国家> countryList = _countryRepository.GetAllCountry();    VAR registerViewModel =新RegisterViewModel
    {
        CountryId = 52,
        CountryList =新的SelectList(countryList,CountryId,国家或地区名称,选择国家)
    };    返回查看(registerViewModel);
}

我有这对我的看法,这行之有效设置所选国家的价值52:

 <%:Html.DropDownListFor(型号=> model.CountryId,Model.CountryList,选择国家)%GT;

然而,当我为这个创建一个模板编辑器,不选择国家的默认值

所以,我改变我的当前视图这样:

 <%:Html.EditorFor(型号=> model.CountryId,新{国家= Model.CountryList})%GT;

然后,我创建我的编辑模板是这样的:

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl< System.Int64>? %GT;
&所述;%= Html.DropDownList(
        的String.Empty / * * /
        (的SelectList)ViewData的[国家],
        选择国家
    )
%GT;


解决方案

我已经通过更换code在我的控制器解决了这个:

  CountryList =新的SelectList(countryList,CountryId,国家或地区名称,52 / *默认国家ID * /)

如果你有更好的解决方案,请让我知道。我会改变接受的答案。

I have this code to set the default value of my select list:

public ActionResult Register()
{
    IList<Country> countryList = _countryRepository.GetAllCountry();

    var registerViewModel = new RegisterViewModel
    {
        CountryId = 52,
        CountryList = new SelectList(countryList, "CountryId", "CountryName", "Select Country")
    };

    return View(registerViewModel);
}

I have this on my view and this works well sets the selected country value to 52:

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

However when I create an editor template for this, the default value for country is not selected

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"], 
        "Select Country"
    )
%>

解决方案

I have solved this by replacing the code in my controller to :

CountryList = new SelectList(countryList, "CountryId", "CountryName",52 /*Default Country Id*/)

If you have better solutions, please let me know. I will change accepted answer.

这篇关于设置选择列表中选择默认值的编辑模板内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆