我想从存储过程绑定我的下拉列表,我的查询返回两个值CountryId和CountryName卡在视图中。 我的模特
public class RadioButton
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
动作
public ActionResult RadioButton()
{
var model = new RadioButton();
List<RadioButton> result = db.Database.SqlQuery<RadioButton>("usp_countryname").ToList();
return View(model);
}
查看
@model MVCLearning.Models.RadioButton
@Html.DropDownList("dd",new SelectListItem(Model.CountryId,Model.CountryName))
On View Model.CountryId是DataValue Field,Model.CountryName是DataTextField之前要传递的内容。
注意:单选按钮操作名称和类名称只是一个示例。
存储过程。
select CountryId,CountryName from tblCountries
通过ViewBag做到这一点它工作得很好但是想用模型来做。 发布了一个类似的问题,但那时我的sql查询只返回一列。 我找了教程,但根据我的要求找不到。
在行动中
model.Countries = db.Database.SqlQuery<IEnumerable<string>("select CountryId,CountryName from tblCountries").ToList();
我在这里得到错误。在SqlQuery&lt;&gt;什么应该通过。