如何使用htmlhelper创建下拉列表 [英] How do I crate a dropdownlist using htmlhelper
问题描述
美好的一天,我想使用htmhelper类创建一个下拉列表,我对此非常陌生并且它已经让人感到困惑。
i有一个列表我想要添加到此下拉列表中的项目以及该项目列表位于通用< list>中
List< string>名称= 新列表< string>();
在我的模型中如图所示
i使用getState()方法将项添加到列表中
namespace newNSMS.Areas.Ministries.Models
{
public class 类别
{
public 列表< string> Name = new List< string>();
}
public class getMethods
{
public static 类别getState(类别d)
{
尝试
{
string countCode = Nig;
char [] chars = {' , '};
testStealthServ.countries csd = new testStealthServ.countries();
List< string> sth = csd.getStates(countCode).ToList< string>();
foreach ( var v in sth)
{
string [] splits = v.Split(chars);
d.pID + = splits.ElementAt( 1 );
d.pName + = splits.ElementAt( 0 );
d.Name.Add(splits.ElementAt( 0 ));
d.ID.Add(splits.ElementAt( 1 ));
}
return d;
}
catch (例外情况)
{
throw ex.InnerException;
}
}
}
}
i想要填充下拉列表我对页面加载的看法所以我在我的控制器类中这样做了
public ActionResult MinistriesHome()
{
类别c = 新类别();
getMethods.getState(c);
return 查看(c);
}
我如何将列表项连接到htmlhelper下拉列表,如下所示
< div class = col-sm-10 >
@ Html.DropDownList(< span class =code-string> schlreg,(SelectList)Model = > Model.Name, null , new {@class = 类别})
< / div >
i真的不知道我在视图中做了什么。表格名称是schlreg。请帮助我如何使用模型列表数据填充视图。
谢谢
< pre lang =c#> @ Html.DropDownList( mylist, new SelectList(Model.Name))
在Model.Name是通用的情况下,这非常有效我的模型类中的列表,其中包含我想要传递到下拉列表中的项目数组
您可以创建强类型的局部视图
< div class = col-sm -10 >
@ Html.DropDownList(schlreg,(SelectList)Model => Model.Name,null,new {@class =类别 })
< / div >
拨打主视图时
@ Html.Partial(〜/ Views / dropdown.cshtml,型号)
good day, i want to create a dropdownlist using the htmhelper classes, i'm very new to this and it's already looking confusing.
i have a list of items that i want to add to this dropdown and that list of items are in a generic <list>
List<string> Name = new List<string>();
in my model as shown
i added the items to the list using the getState() method
namespace newNSMS.Areas.Ministries.Models
{
public class Category
{
public List<string> Name = new List<string>();
}
public class getMethods
{
public static Category getState(Category d)
{
try
{
string countCode = "Nig";
char[] chars = { ',' };
testStealthServ.countries csd = new testStealthServ.countries();
List<string> sth = csd.getStates(countCode).ToList<string>();
foreach (var v in sth)
{
string[] splits = v.Split(chars);
d.pID += splits.ElementAt(1);
d.pName += splits.ElementAt(0);
d.Name.Add(splits.ElementAt(0));
d.ID.Add(splits.ElementAt(1));
}
return d;
}
catch (Exception ex)
{
throw ex.InnerException;
}
}
}
}
i want the dropdownlist to be populated in my view on page load so i did this in my controller class
public ActionResult MinistriesHome()
{
Category c = new Category();
getMethods.getState(c);
return View(c);
}
how do i connect the list items to the htmlhelper dropdownlist as shown below
<div class="col-sm-10">
@Html.DropDownList("schlreg", (SelectList)Model => Model.Name, null, new { @class = "Category" })
</div>
i really have no idea what i'm doing in the view. the form name is "schlreg". please assist me on how to populate the view with the model list data.
Thanks
@Html.DropDownList("mylist", new SelectList(Model.Name))
this worked perfectly well where Model.Name is a generic list in my model class that has an array of items that i want to pass into the dropdownlist
You can create strongly type partial view
<div class="col-sm-10"> @Html.DropDownList("schlreg", (SelectList)Model => Model.Name, null, new { @class = "Category" }) </div>
when you call the main view
@Html.Partial("~/Views/dropdown.cshtml", model)
这篇关于如何使用htmlhelper创建下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!