简体   繁体   English

如何使模型中的值与下拉列表中选择的值匹配?

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

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. 我的视图类型为:@model SchoolIn.Models.Student,但我想插入另一个模型Model.Enrollments并最小化下拉列表项。

  <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: LookupUtils是一个静态类,具有:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在控制器中访问视图的下拉列表的值? - How can I access the values of my view's dropdown list in my controller? 如何从主页访问局部视图下拉列表的选定值? - How can I access the selected value of my partial view dropdown list from the main page? 如果我的列表在ViewBag中,如何使我的下拉列表保留选定的项目? - How can I make my dropdown list retain selected item if my list is in the ViewBag? 我如何在模型中具有必需的和非序列化的属性 - How can I have a required and nonSerialized attribute in my model MVC:如何确保我的字段在编辑表单的视图内选择了值? - MVC: How can I make sure that my field have the value selected inside my view on the edit form? 如何从ActionExecuting过滤器中获取路由值? - How can I get route values from inside my ActionExecuting filter? 如何在SelectListItem和DropDownList中保留数据库值列表? - How can I but this list of database values in my SelectListItem and DropDownList? 如何以我的操作方法从数据库中检索对象属性值? - How can i retrieve the object properties values from the database, in my action method? 如何从ASP.NET MVC 3控制器中的ListBox获取值? - How can I get the values from a ListBox in my ASP.NET MVC 3 controller? 如何从视图中获得双倍到我的 Model? - How can I get a double from the View into My Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM