简体   繁体   中英

MVC dropdownlist wrong display

I am maing an MVC application connected to Entity Framework. 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. That is why the last value is kept saved for taking names it only hits the same 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>
                            }

                        }
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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