使用Data Entity Framework,我创建了添加我的数据库。 我有一个名为Droptest的表,请参阅模型条目:
namespace DropMenu4FEB2018_FINAL.Models
{
using System;
using System.Collections.Generic;
public partial class DropTest
{
public string DisplayList { get; set; }
public string DisplayIndex { get; set; }
}
}
控制器代码:
Public action Create ()
{
DROPMENUEntities db = new DROPMENUEntities();
List<DropTest> list = db.DropTests.ToList();
ViewBag.DropTestList = new SelectList(list, "DisplayIndex", "DisplayList");
Return view ();
}
查看代码:
@ Html.LabelFor(model =&gt; model.Car,htmlAttributes:new {@class =&#34; control-label col-md-2&#34;}) @ Html.DropDownListFor(model =&gt; model.Car,ViewBag.DropTestList as SelectList,&#34; - SELECT ONE - &#34;,new {@class =&#34; form-control&#34; @ Html.ValidationMessageFor(model =&gt; model.Car,&#34;&#34;,new {@class =&#34; text-danger&#34;})以上控制器和视图代码正在运行。
现在,我决定如果我使用存储过程返回表的内容(以支持将来的过滤输出),它会更灵活。我创建了存储过程(在此阶段基本),我在SSMS中调用了DisplayCars,它被数据实体框架命名为DisplayCars_Result 在模型中,您可以查看存储过程条目:
namespace DropMenu4FEB2018_FINAL.Models
{
using System;
public partial class DisplayCars_Result
{
public string DisplayList { get; set; }
public string DisplayIndex { get; set; }
}
}
我无法确定用控制器方法Create中的存储过程引用替换表引用的语法(下面的更改不起作用)
public ActionResult Create()
{
//DROPMENUEntities db = new DROPMENUEntities();
//List<DropTest> list = db.DropTests.ToList();
//ViewBag.DropTestList = new SelectList(list, "DisplayIndex", "DisplayList");
DROPMENUEntities DROPMENUEntities = new DROPMENUEntities();
List<DisplayCars_Result> list = DROPMENUEntities.DisplayCars.ToList();
ViewBag.DisplayCars = new SelectList(list, "DisplayIndex", "DisplayList");
return View();
}
我们将非常感激地提供任何帮助。谢谢。
答案 0 :(得分:0)
这是对List语句的一个简单修复,即后缀存储过程名称DisplayCars with(),见下文:
public ActionResult Create()
{
DROPMENUEntities db = new DROPMENUEntities();
List<DisplayCars_Result> list = db.DisplayCars().ToList();
ViewBag.DisplayCars = new SelectList(list, "DisplayIndex", "DisplayList");
return View();
}
现在按预期工作。干杯。