简体   繁体   English

MVC下拉列表显示错误

[英]MVC dropdownlist wrong display

I am maing an MVC application connected to Entity Framework. 我正在连接到实体框架的MVC应用程序。 In my view I have a dropdown list. 在我看来,我有一个下拉列表。 Code looks like this: 代码如下:

@{
    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>
                                }

                            }
                        }

However, in my dropdown list I see wrong things. 但是,在下拉列表中我看到了错误的信息。 The number of elements is correct, but all the names are the same, all the names correspond to first matching 'group' to pick from model. 元素的数量是正确的,但是所有名称都相同,所有名称都对应于要从模型中选择的第一个匹配“组”。 What do I do wrong? 我做错了什么?
My controller looks like this: 我的控制器如下所示:

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

Apparently, adding groups does not work well, groups are not unique (however in database they are). 显然,添加组效果不佳,组不是唯一的(但是在数据库中却是唯一的)。 What is wrong with it? 怎么了

You are not passing any value for model.subject_id from controller. 您没有从控制器传递model.subject_id的任何值。 That is why the last value is kept saved for taking names it only hits the same 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>
                            }

                        }
                    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM