剃刀Html.DropdownList提供“请选择”作为选项标签AND“US”作为selectedValue [英] razor Html.DropdownList provide "Please select" as optionLabel AND "US" as selectedValue

查看:129
本文介绍了剃刀Html.DropdownList提供“请选择”作为选项标签AND“US”作为selectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据

  var countries = new Dictionary< string,string>() 
countries.Add(AF,阿富汗);
countries.Add(US,United States);
countries.Add(FR,France);

剃刀

  @ Html.DropdownList(Countries,new SelectList(countries,key,value,US),Select Country)

问题:在我的剃须刀中,我指定US / code>为selectedValue,而选择国家为optionLabel。我希望这可以插入选择国家作为下拉列表中的第一个项目,并且已经选择了US



实际发生的情况选择国家已插入,但US未被选中。 选择国家是。



问题:我可以同时拥有吗?剃须刀注入请选择,而且还预先选择US作为默认值。



请注意:我正在从我的数据库中的表格中获取数据,所以我不希望在选择,或者将其自己插入到字典中。在我的控制器中做Model.Countries = dbRepo.GetCountries()更容易,声明一个空字典,添加请选择,然后循环通过.GetCountries()并添加到原始字典,然后再将其设置为Model.Countries我的观点。



谢谢!

解决方案

不知道为什么它不是因为你没有显示你的实际代码。在您的实际数据集中,可能没有选择 key =US



但是无论如何,让我用一个例子来说明我会推荐使用视图模型和强类型帮助器的方法 DropDownListFor helper。



模型:

  public class CountriesViewModel 
{
public string SelectedCountryCode {get;组; }
public IEnumerable< SelectListItem>国家{get;组;
}

控制器:

  public class HomeController:Controller 
{
public ActionResult Index()
{
var model = new CountriesViewModel();
// TODO:显然这些将来自你的数据库
model.Countries = new []
{
new SelectListItem {Value =AF,Text =Afghanistan} ,
new SelectListItem {Value =US,Text =United States},
new SelectListItem {Value =FR,Text =France},
};

//用Value =US
//预先选择该选项确保您在国家列表中有这样的选项
model.SelectedCountryCode =US;
return View(model);
}
}

查看(〜/ Views /Home/Index.cshtml ):

  @model CountriesViewModel 

@ Html.DropDownListFor(
x => x.SelectedCountryCode,
Model.Countries,
选择国家

pre>

结果:




Data:

var countries = new Dictionary<string,string>();
countries.Add("AF","Afghanistan");
countries.Add("US","United States");
countries.Add("FR","France");

Razor:

@Html.DropdownList("Countries",new SelectList(countries, "key", "value", "US"), "Select Country")

Issue: in my razor, I'm specifying "US" for the selectedValue, and "Select Country" for optionLabel. I expect this to insert "Select Country" as the first item in the dropdown, AND have "US" already selected for me.

What actually happens: "Select Country" is inserted, but "US" is NOT selected. "Select Country" is.

Question: can I have both? Where Razor injects "Please Select", but also pre-selects "US" as the default?

Please note: I'm getting the data from a table in my database, so I'd prefer not to have an actual row in there with "Please select", or have to insert it myself into the dictionary. It's easier to do Model.Countries = dbRepo.GetCountries() in my controller vs. declaring an empty dictionary, adding "Please Select", then looping thru .GetCountries() and adding to the original dictionary before setting it to Model.Countries for my view.

Thanks!

解决方案

This should work. No idea why it doesn't because you haven't shown your actual code. Probably there's no option with key = "US" in your actual dataset.

But anyway let me illustrate with an example the approach that I would recommend which is to use a view model and a strongly typed helper DropDownListFor helper.

Model:

public class CountriesViewModel
{
    public string SelectedCountryCode { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new CountriesViewModel();
        // TODO: obviously those will come from your database
        model.Countries = new[]
        {
            new SelectListItem { Value = "AF", Text = "Afghanistan" },
            new SelectListItem { Value = "US", Text = "United States" },
            new SelectListItem { Value = "FR", Text = "France" },
        };

        // Preselect the option with Value = "US"
        // Make sure you have such option in the Countries list
        model.SelectedCountryCode = "US";
        return View(model);
    }
}

View (~/Views/Home/Index.cshtml):

@model CountriesViewModel

@Html.DropDownListFor(
    x => x.SelectedCountryCode,
    Model.Countries, 
    "Select Country"
)

Result:

这篇关于剃刀Html.DropdownList提供“请选择”作为选项标签AND“US”作为selectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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