C# 硬编码列表中的两项

C# 硬编码列表中的两项,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,这是我的剃须刀: @Html.DropDownListFor(m => m.CountryCode, ???? , new { @class = "form-control styled-select", id = "CountryCode", name = "CountryCode" }) 如何在参数中创建选定的“美国”和“加拿大”列表项?我无法正确理解语法 将新属性添加到视图模型以存储国家/地区列表。此属性的类型可以是SelectListItem的集合 public class C

这是我的剃须刀:

@Html.DropDownListFor(m => m.CountryCode,  ???? , new { @class = "form-control styled-select", id = "CountryCode", name = "CountryCode" })

如何在参数中创建选定的“美国”和“加拿大”列表项?我无法正确理解语法

将新属性添加到视图模型以存储国家/地区列表。此属性的类型可以是
SelectListItem
的集合

public class CreateSomething
{
   public List<SelectListItem> Countries { set;get;}
   //Your existing properties
   public string CountryCode { set;get; }
}
public类创建一些东西
{
公共列表国家{set;get;}
//你现有的财产
公共字符串CountryCode{set;get;}
}
在GET操作中,只需使用一些数据加载此属性

public ActionResult Create()
{
   var vm= new CreateSomething();
   vm.Countries= new List<SelectListItem> {
                                        new SelectListItem { Value="USA", Text="USA"},
                                        new SelectListItem { Value="CANADA", Text="Canada"}
   };
   return View(vm);
}
public ActionResult Create()
{
var vm=newcreatesomething();
vm.Countries=新列表{
新建SelectListItem{Value=“USA”,Text=“USA”},
新建SelectListItem{Value=“CANADA”,Text=“CANADA”}
};
返回视图(vm);
}
在你看来

@model CreateSomething
@using(Html.BeginForm())
{
   <label> Select a country </label>
   @Html.DropDownListFor(m => m.CountryCode,Model.Countries)
   <input type="submit" />
}
@model-CreateSomething
@使用(Html.BeginForm())
{
选择一个国家
@DropDownListFor(m=>m.CountryCode,Model.Countries)
}

将新特性添加到视图模型以存储国家/地区列表。此属性的类型可以是
SelectListItem
的集合

public class CreateSomething
{
   public List<SelectListItem> Countries { set;get;}
   //Your existing properties
   public string CountryCode { set;get; }
}
public类创建一些东西
{
公共列表国家{set;get;}
//你现有的财产
公共字符串CountryCode{set;get;}
}
在GET操作中,只需使用一些数据加载此属性

public ActionResult Create()
{
   var vm= new CreateSomething();
   vm.Countries= new List<SelectListItem> {
                                        new SelectListItem { Value="USA", Text="USA"},
                                        new SelectListItem { Value="CANADA", Text="Canada"}
   };
   return View(vm);
}
public ActionResult Create()
{
var vm=newcreatesomething();
vm.Countries=新列表{
新建SelectListItem{Value=“USA”,Text=“USA”},
新建SelectListItem{Value=“CANADA”,Text=“CANADA”}
};
返回视图(vm);
}
在你看来

@model CreateSomething
@using(Html.BeginForm())
{
   <label> Select a country </label>
   @Html.DropDownListFor(m => m.CountryCode,Model.Countries)
   <input type="submit" />
}
@model-CreateSomething
@使用(Html.BeginForm())
{
选择一个国家
@DropDownListFor(m=>m.CountryCode,Model.Countries)
}

硬编码选项

如果确实需要硬编码选项,可以创建一个包含两个目标值的
SelectList

@Html.DropDownListFor(m => m.CountryCode, new SelectList(new []{ "USA","CANADA" }), ...)
或者只是显式地创建一个
元素来为您处理它,这可能比使用实际的HTML帮助程序更具可读性:

<select id='CountryCode' name='CountryCode' class='form-control styled-select'>
    <option>USA</option>
    <option>CANADA</option>
</select>

美国
加拿大
否则,请使用型号


如果这些选项看起来不适合你的场景,你应该考虑使用更多的基于MVC的方法,比如创建一个模型来存储这些属性,或者将它们存储在一个容器中,比如<代码> ViewBag < /代码>,并将它们传递到你的视图中,类似于

< P> <强>硬编码选项>/P> 如果确实需要硬编码选项,可以创建一个包含两个目标值的
SelectList

@Html.DropDownListFor(m => m.CountryCode, new SelectList(new []{ "USA","CANADA" }), ...)
或者只是显式地创建一个
元素来为您处理它,这可能比使用实际的HTML帮助程序更具可读性:

<select id='CountryCode' name='CountryCode' class='form-control styled-select'>
    <option>USA</option>
    <option>CANADA</option>
</select>

美国
加拿大
否则,请使用型号

如果这些选项看起来不适合你的场景,你应该考虑使用更多的基于MVC的方法,比如创建一个模型来存储这些属性,或者将它们存储在一个容器中,比如<代码> ViewBag < /代码>,并将它们传递到你的视图中,类似于