本文介绍了我怎样才能从我模型中的值我的下拉列表匹配所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能从我的模型匹配所选值的值在我的下拉列表?

我想这样做是有下拉列表反映模型的属性值。我只能够在所有下拉列表中显示的值相同。我一直在试图做到这一点:

  @ Html.DropDownList(搜索字符串,Model.Enrollments.FirstOrDefault()WeekDays.Select(S =>
   新SelectListItem的{text = s.ToString(),值= s.ToString()
   选择= s.ToString()。等于(Model.Enrollments.FirstOrDefault()。classDays)}))

但没有运气。

我的模型招生的一个片段:

 公共字符串[] =平日新的字符串[6] {日,星期一,星期二,星期三,星期四,星期五};    公共字符串[]平日    {        {返回工作日; }    }

我的看法是类型:@model SchoolIn.Models.Student但我想插话另一种模式,Model.Enrollments和minipulate下拉列表项

 <表>
     &所述; TR>
              @ {
     INT CNT = 0;
        清单< SchoolIn.ViewModels.EnrolledCourseData>课程= ViewBag.Courses;        的foreach(在课程VAR课程)
        {
            如果(CNT ++%4 == 0)
            {                       @:或其可/ TR> &所述; TR>
             }
                       @:其中; TD>
                      < BR />< BR />< BR />
                    <输入类型=复选框
                           NAME =selectedCourses
                           值=@ course.CourseID
                           @(?Html.Raw(course.Assigned检查= \\检查\\:)))   />
                    @ * course.CourseID @:@ course.Title * @
                    @ course.Title< BR /> < BR />      如果(Model.Enrollments!= NULL)
      {       @ Html.DropDownList(searchString的,Model.Enrollments.FirstOrDefault()WeekDays.Select(S =方式>新SelectListItem {文本= s.ToString(),值= s.ToString(),选定= s.ToString( ).Equals(Model.Enrollments.FirstOrDefault()。classDays)}))     }
                                 @:其中; / TD>       }
                                @:或其可/ TR> }          < /表>


解决方案

  @ Html.DropDownListFor(X => x.courseId,新的SelectList(LookupUtils.AvailableCourcesList(),价值,文本,Model.courseId))

LookupUtils是一个静态类有:

 公共静态列表< SelectListItem> AvailableCourcesList()
{
    VAR的DataContext =新YourDataContext();
    。VAR数据= dataContext.GetCourcesFn()了ToList();    VAR的结果=(从数据资源
                   选择新SelectListItem()
                              {
                                  文= res.courseName,
                                  值= res.courseId.ToString()
                              }).ToList();    返回结果;
}

How can I have the values from my model match the selected values in my dropdown lists?

What I would like to do is to have the dropdown list reflect the property value of the model. I've only been able to show the same value in all of the dropdown lists. I've been trying to do this:

@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().WeekDays.Select(s =>
   new SelectListItem { Text = s.ToString(), Value = s.ToString(),
   Selected = s.ToString().Equals(Model.Enrollments.FirstOrDefault().classDays) }))

but with no luck.

A snippet of my Enrollment model:

    public string[] weekDays = new string[6] { "Day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

    public string[] WeekDays

    {

        get { return weekDays; }

    }

My view is of type: @model SchoolIn.Models.Student but I would like to interject another model, Model.Enrollments and minipulate the dropdown list items.

  <table>
     <tr>
              @{
     int cnt = 0;
        List<SchoolIn.ViewModels.EnrolledCourseData> courses = ViewBag.Courses;

        foreach (var course in courses)
        {
            if (cnt++ % 4 == 0)
            {

                       @:  </tr> <tr>


             }
                       @: <td >
                      <br /><br /><br />
                    <input type="checkbox"
                           name="selectedCourses"
                           value="@course.CourseID"
                           @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")))

   />


                    @*course.CourseID @:  @course.Title*@
                    @course.Title<br />  <br />

      if (Model.Enrollments != null)
      {



       @Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals(Model.Enrollments.FirstOrDefault().classDays) }))

     }




                                 @:</td>



       }
                                @:</tr>

 }

          </table>
解决方案
@Html.DropDownListFor( x => x.courseId, new SelectList(LookupUtils.AvailableCourcesList(), "Value", "Text", Model.courseId))

LookupUtils is a static class have:

public static List<SelectListItem> AvailableCourcesList()
{
    var dataContext = new YourDataContext(  );
    var data = dataContext.GetCourcesFn().ToList();

    var result = ( from res in data
                   select new SelectListItem()
                              {
                                  Text = res.courseName,
                                  Value = res.courseId.ToString()
                              } ).ToList();

    return result;
}

这篇关于我怎样才能从我模型中的值我的下拉列表匹配所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:43