我有一个存储过程,该存储过程从数据库中检索现有数据,并且我想将DropDownListFor
设置为一个等于该存储过程结果中的数据的值。这是用于编辑模块的。
这是我的控制器代码:
[HttpPost]
public ActionResult EditGetProjDetails(int id)
{
var vm = new ProjectsViewModel();
sp_GetProjectDetails_Result result = db_RIRO.sp_GetProjectDetails(id).FirstOrDefault();
vm.BusinessLineList = db_RIRO.sp_GetAllBusinessLine()
.Select(a => new SelectListItem
{
Value = a.BusinessLineID.ToString(),
Text = a.BusinessLine,
Selected = // what code do i put here?
})
.ToList();
vm.ProjectID = result.ProjectID;
vm.BusinessUnit = result.BusinessUnit;
return PartialView("~/Views/Project/_EditProjDetails.cshtml", vm);
这是我的观点:
@Html.DropDownListFor(a => a.BusinessLineID, Model.BusinessLineList, new { @class = "form-control input-sm", placeholder = "Business Line", required = "required", autocomplete = "off" })
这是我的ViewModel:
public int BusinessLineID { get; set; }
public List<SelectListItem> BusinessLineList { get; set; }
public string BusinessLine { get; set; }
解决方案:
var vm = new ProjectsViewModel()
{
BusinessLineID = 2
};
sp_GetProjectDetails_Result result = db_RIRO.sp_GetProjectDetails(id).FirstOrDefault();
vm.BusinessLineList = db_RIRO.sp_GetAllBusinessLine()
.Select(a => new SelectListItem
{
Value = a.BusinessLineID.ToString(),
Text = a.BusinessLine
})
.ToList();
return PartialView("~/Views/Project/_EditProjDetails.cshtml", vm);
答案 0 :(得分:1)
您要遍历所有业务线,然后创建一个带有每个业务线的值和文本的选择列表项列表。
sp_GetProjectDetails_Result结果具有您要选择的单个业务ID?
如果是,则在ID中与过程中的结果ID相同的情况下,在循环中为每个业务线将选择列表项的selected属性设置为true。
类似的东西:
Selected = (a.BusinessLineID == result.ID) //or whatever result is from the procedure