MVC下拉列表显示错误

时间:2016-12-29 22:40:31

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在使用连接到Entity Framework的MVC应用程序。在我看来,我有一个下拉列表。代码如下所示:

@{
    ViewBag.Title = "ClassesPickGroup"; } @model ClassDeclarationsThsesis.Models.ClassesPickGroupViewModel

<h2>ClassesPickGroup</h2>


@foreach (var user in Model.users) {
    if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
    {
        if (user.user_type.Replace(" ", String.Empty) == 3.ToString() || user.user_type.Replace(" ", String.Empty) == 2.ToString())
        {
            using (Html.BeginForm("ClassesPickGroup", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Generate summary views</h4>
                <hr />
                @Html.ValidationSummary("", new { @class = "text-danger" })
                <div class="form-group">
                    @{
                        List<SelectListItem> listItems1 = new List<SelectListItem>();
                        foreach (var sub in Model.subjects)
                        {
                            if (sub.name.Replace(" ", String.Empty) == Model.subject_name.Replace(" ", String.Empty))
                            {
                                Model.subject_id = sub.class_id;
                            }
                        }
                        foreach (var group in Model.groups)
                        {
                            if (group.class_id == Model.subject_id)
                            {
                                listItems1.Add(new SelectListItem
                                {
                                    Text = group.name,
                                    Value = group.name,
                                });
                            }
                        }
                    }
                    @Html.LabelFor(m => m.selected_group, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.selected_group, listItems1, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" class="btn btn-default" value="Submit" />
                    </div>
                </div>

                            }
                        }
                        if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
                        {
                            <p>You do not have enough permissions to enter this page. Contact the administrator.</p>
                                }

                            }
                        }

但是,在我的下拉列表中,我看到了错误的内容。元素的数量是正确的,但所有名称都相同,所有名称都对应于从模型中选择的第一个匹配的“组”。我做错了什么? 我的控制器看起来像这样:

public ActionResult ClassesPickGroup(ClassesPickGroupViewModel value)
        {
            ClassDeclarationsDBEntities1 entities=new ClassDeclarationsDBEntities1();
            int subj_id=0;
            ClassesPickGroupViewModel model=new ClassesPickGroupViewModel();
            model.subject_name = value.subject_name;
            foreach (var subject in entities.Subjects)
            {
                if(subject.name.Replace(" ",String.Empty)==value.subject_name.Replace(" ", String.Empty))
                {
                    subj_id = subject.class_id;
                }
            }
            model.groups = entities.Groups.ToList();
            model.subjects = entities.Subjects.ToList();
            model.users = entities.Users.ToList();
            if (ModelState.IsValid)
            {
                return RedirectToAction("ClassesView", "Account");
            }
            else
            {
                model.groups = entities.Groups.ToList();
                model.subjects = entities.Subjects.ToList();
                model.users = entities.Users.ToList();
                return View(model);
            }
            return View(model);
        }  

显然,添加组不能很好地工作,组不是唯一的(但在数据库中却是如此)。这有什么问题?

1 个答案:

答案 0 :(得分:1)

您没有从控制器传递model.subject_id的任何值。这就是为什么保留最后一个值以获取名称只能命中相同的subject_id

@{
ViewBag.Title = "ClassesPickGroup"; } @model ClassDeclarationsThsesis.Models.ClassesPickGroupViewModel

<h2>ClassesPickGroup</h2>


@foreach (var user in Model.users) {
if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
{
    if (user.user_type.Replace(" ", String.Empty) == 3.ToString() || user.user_type.Replace(" ", String.Empty) == 2.ToString())
    {
        using (Html.BeginForm("ClassesPickGroup", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <h4>Generate summary views</h4>
            <hr />
            @Html.ValidationSummary("", new { @class = "text-danger" })
            <div class="form-group">
                @{
                    List<SelectListItem> listItems1 = new List<SelectListItem>();
                    foreach (var sub in Model.subjects)
                    {
                        if (sub.name.Replace(" ", String.Empty) == Model.subject_name.Replace(" ", String.Empty))
                        {
                            Model.subject_id = sub.class_id;
                        }
                            foreach (var group in Model.groups)
                           {
                              if (group.class_id == Model.subject_id)
                              {
                                listItems1.Add(new SelectListItem
                               {
                                  Text = group.name,
                                  Value = group.name,
                                 });
                              }
                           }
                        }
                    }

                }
                @Html.LabelFor(m => m.selected_group, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.DropDownListFor(m => m.selected_group, listItems1, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" class="btn btn-default" value="Submit" />
                </div>
            </div>

                        }
                    }
                    if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
                    {
                        <p>You do not have enough permissions to enter this page. Contact the administrator.</p>
                            }

                        }
                    }