为什么我的 DropDownList 中出现 "System.Web.Mvc.SelectListItem"?
Why I am getting "System.Web.Mvc.SelectListItem" in my DropDownList?
我相信我已经正确绑定了我的数据,但我似乎无法让每个 SelectListItem 的文本 属性 正确显示。
我的模特:
public class Licenses
{
public SelectList LicenseNames { get; set; }
public string SelectedLicenseName { get; set; }
}
控制器:
[HttpGet]
public ActionResult License()
{
try
{
DataTable LicsTable = BW.SQLServer.Table("GetLicenses", ConfigurationManager.ConnectionStrings["ProfressionalActivitiesConnection"].ToString());
ProfessionalActivities.Models.Licenses model = new ProfessionalActivities.Models.Licenses();
model.LicenseNames = new SelectList(LicsTable.AsEnumerable().Select(row =>
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}));
return PartialView("_AddLicense", model);
}
catch (Exception ex)
{
var t = ex;
return PartialView("_AddLicense");
}
}
查看:
@Html.DropDownList("LicenseNames", new SelectList(Model.LicenseNames, "Value", "Text", Model.LicenseNames.SelectedValue), new { htmlAttributes = new { @class = "form-control focusMe" } })
使用 LicenseNames
属性 的 Items
属性 类型 SelectList
@Html.DropDownList("SelectedLicenseName", new SelectList(Model.LicenseNames.Items,
"Value", "Text", Model.LicenseNames.SelectedValue))
或者使用 DropDownListFor
辅助方法
@Html.DropDownListFor(d=>d.SelectedLicenseName,
Model.LicenseNames.Items as List<SelectListItem>)
因此,当您 post 您的表单时,您可以检查 SelectedLicenseName
属性
[HttpPost]
public ActionResult Create(Licenses model)
{
//check model.SelectedLicenseName
}
我明确设置了 dataValueField
和 dataTextField
名称。
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}), "Value", "Text");
那么就没有必要在您的观点中写下 Model.LicenseNames.Items as List<SelectListItem>
(按照您接受的答案中的建议)。
我相信我已经正确绑定了我的数据,但我似乎无法让每个 SelectListItem 的文本 属性 正确显示。
我的模特:
public class Licenses
{
public SelectList LicenseNames { get; set; }
public string SelectedLicenseName { get; set; }
}
控制器:
[HttpGet]
public ActionResult License()
{
try
{
DataTable LicsTable = BW.SQLServer.Table("GetLicenses", ConfigurationManager.ConnectionStrings["ProfressionalActivitiesConnection"].ToString());
ProfessionalActivities.Models.Licenses model = new ProfessionalActivities.Models.Licenses();
model.LicenseNames = new SelectList(LicsTable.AsEnumerable().Select(row =>
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}));
return PartialView("_AddLicense", model);
}
catch (Exception ex)
{
var t = ex;
return PartialView("_AddLicense");
}
}
查看:
@Html.DropDownList("LicenseNames", new SelectList(Model.LicenseNames, "Value", "Text", Model.LicenseNames.SelectedValue), new { htmlAttributes = new { @class = "form-control focusMe" } })
使用 LicenseNames
属性 的 Items
属性 类型 SelectList
@Html.DropDownList("SelectedLicenseName", new SelectList(Model.LicenseNames.Items,
"Value", "Text", Model.LicenseNames.SelectedValue))
或者使用 DropDownListFor
辅助方法
@Html.DropDownListFor(d=>d.SelectedLicenseName,
Model.LicenseNames.Items as List<SelectListItem>)
因此,当您 post 您的表单时,您可以检查 SelectedLicenseName
属性
[HttpPost]
public ActionResult Create(Licenses model)
{
//check model.SelectedLicenseName
}
我明确设置了 dataValueField
和 dataTextField
名称。
new SelectListItem
{
Value = row["Description"].ToString(),
Text = "test"
}), "Value", "Text");
那么就没有必要在您的观点中写下 Model.LicenseNames.Items as List<SelectListItem>
(按照您接受的答案中的建议)。