如何发布列表然后在我的控制器中访问它?

时间:2012-10-28 17:35:32

标签: asp.net-mvc-3

我在模型中创建了一个list属性,如此

public virtual List<String> listOfDays { get; set; }

然后我转换并将其存储在列表中,如下所示:

for (int i = 0; i < 30 i++)
{
            var enrollment = new Enrollment();
            enrollment.StudentID = id;
            enrollment.listOfDays = searchString.ToList(); 
            db.Enrollments.Add(enrollment);
            db.SaveChanges();
}

我在这里放了一个断点...... enrollment.listOfDays = searchString.ToList(); ......一切都很好。我看到转换已执行,我可以看到listOfDays中的值。

所以我想我会在我的数据库中找到一个名为listOfDays的列,因为我先做代码,但属性不存在。

然后我想我会尝试这样访问它......

var classdays = from e in db.Enrollments where e.StudentID == id select e.listOfDays;

var days = classdays.ToList(); 
//here I get an error message about this not being supported in Linq.

问题:

  • 为什么您认为该属性不在数据库中?
  • 如何将此数组发布到我的模型,然后在控制器中访问它?

感谢您的帮助。

感谢Decker:http://forums.asp.net/members/Decker%20Dong%20-%20MSFT.aspx

以下是它的工作原理:

在此处使用表单集合...

在[HttpPost]中......

private void Update(FormCollection formCollection,int id)

for(int sc = 0; sc&lt; theSelectedCourses.Count(); sc ++)             {   var enrollment = new Enrollment();                 enrollment.CourseID = Convert.ToInt32(theSelectedCourses [sc]);                 enrollment.StudentID = id;    enrollment.listOfDays = formCollection [“searchString”]; //将其绑定为字符串而不是列表或数组。

然后在[HttpGet] ......

private void PopulateAssignedenrolledData(Student student,int id)         {

var dayList =来自db.Enrollments中的e,其中e.StudentID == id选择e;

var days = dayList.ToList();             if(days.Count&gt; 0)             {                 string dl = days.FirstOrDefault()。listOfDays;                 string [] listofdays = dl.Split(',');                 ViewBag.classDay = listofdays.ToList();             }

1 个答案:

答案 0 :(得分:0)

感谢Decker:http://forums.asp.net/members/Decker%20Dong%20-%20MSFT.aspx

以下是它的工作原理:

在此处使用表单集合...

在[HttpPost]中......

private void Update(FormCollection formCollection,int id)

for(int sc = 0; sc&lt; theSelectedCourses.Count(); sc ++)             {   var enrollment = new Enrollment();                 enrollment.CourseID = Convert.ToInt32(theSelectedCourses [sc]);                 enrollment.StudentID = id;    enrollment.listOfDays = formCollection [“searchString”]; //将其绑定为字符串而不是列表或数组。

然后在[HttpGet] ......

private void PopulateAssignedenrolledData(Student student,int id)         {

var dayList =来自db.Enrollments中的e,其中e.StudentID == id选择e;

var days = dayList.ToList();             if(days.Count&gt; 0)             {                 string dl = days.FirstOrDefault()。listOfDays;                 string [] listofdays = dl.Split(',');                 ViewBag.classDay = listofdays.ToList();             }