我正在尝试使用LINQ查询的结果在MVC应用程序中创建下拉列表。 我正在使用这个答案作为参考。 但是,当我尝试为我的情况实现时,我得到错误: 'System.String' does not contain a property with the name 'SAMPLING_EVENT'
我的代码如下:
调节器
public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { ViewBag.Title = "Sample Tracker Tool"; DateTime nineMonthsAgo = DateTime.Now.AddDays(-270); var context = new EDMS_Entities(); var resultSet = (from samplingEvents in context.EDMS_SAMPLES where samplingEvents.RECORD_CREATED_DATE >= nineMonthsAgo orderby samplingEvents.SAMPLING_EVENT select samplingEvents.SAMPLE_ID) .Distinct(); var viewModel = new SamplingEventsVM(); viewModel.SamplingEvents = new SelectList(resultSet, "SAMPLING_EVENT", "SAMPLING_EVENT"); return View(viewModel); } }ViewModel类
public class SamplingEventsVM { public int SelectedSamplingEvent { get; set; } public SelectList SamplingEvents { get; set; } }视图
@model SamplingEventsVM <h2>@ViewBag.Title</h2> <span> @Html.DropDownListFor(model => model.SelectedSamplingEvent, Model.SamplingEvents, "Sampling Event") </span>我究竟做错了什么?
I'm trying to use the results of a LINQ query to create a dropdownlist in an MVC app. I'm using this answer as a reference. However, when I try to implement for my case I get the error: 'System.String' does not contain a property with the name 'SAMPLING_EVENT'
My code is as follows:
Controller
public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { ViewBag.Title = "Sample Tracker Tool"; DateTime nineMonthsAgo = DateTime.Now.AddDays(-270); var context = new EDMS_Entities(); var resultSet = (from samplingEvents in context.EDMS_SAMPLES where samplingEvents.RECORD_CREATED_DATE >= nineMonthsAgo orderby samplingEvents.SAMPLING_EVENT select samplingEvents.SAMPLE_ID) .Distinct(); var viewModel = new SamplingEventsVM(); viewModel.SamplingEvents = new SelectList(resultSet, "SAMPLING_EVENT", "SAMPLING_EVENT"); return View(viewModel); } }ViewModel class
public class SamplingEventsVM { public int SelectedSamplingEvent { get; set; } public SelectList SamplingEvents { get; set; } }View
@model SamplingEventsVM <h2>@ViewBag.Title</h2> <span> @Html.DropDownListFor(model => model.SelectedSamplingEvent, Model.SamplingEvents, "Sampling Event") </span>What am I doing wrong?
最满意答案
您正在选择此select samplingEvents.SAMPLE_ID
所以你得到一个int列表,取决于你的ID类型
然后,您尝试使用属性值"SAMPLING_EVENT"创建一个选择列表
在您填充resultSet的int对象上不存在。
而是这样做:
var resultSet = (from samplingEvents in context.EDMS_SAMPLES where samplingEvents.RECORD_CREATED_DATE >= nineMonthsAgo orderby samplingEvents.SAMPLING_EVENT select samplingEvents) .Distinct();You are selecting this select samplingEvents.SAMPLE_ID
So you get a List of int maybe, depends on your ID type
Then you try to make a select list with the property value "SAMPLING_EVENT"
Which doesn't exist on the int object you filled resultSet with.
Instead do this:
var resultSet = (from samplingEvents in context.EDMS_SAMPLES where samplingEvents.RECORD_CREATED_DATE >= nineMonthsAgo orderby samplingEvents.SAMPLING_EVENT select samplingEvents) .Distinct();更多推荐
发布评论