在设置中选择的SelectList属性在ASP.NET的基础上选定的ID MVC 3中收集的每个对象 [英] Setting selected property in SelectList for each object in collection based on selected ID in ASP.NET MVC 3

查看:254
本文介绍了在设置中选择的SelectList属性在ASP.NET的基础上选定的ID MVC 3中收集的每个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后续问题:
<一href=\"http://stackoverflow.com/questions/13713119/populate-a-list-property-from-delimited-string/13713238#13713238\">Populate从分隔字符串列表属性

我遇到的问题是在我的编辑视图中的下拉框不保留/选择基于BugAssignment模型中的AssignedUserID属性正确的项目。

The problem I'm having is the DropDown boxes in my Edit view are not retaining/selecting the the proper item based on the AssignedUserID property within BugAssignment model.

模型:

public class BugAssignment
{
    public int BugAssignmentID { get; set; }
    public int BugNumber { get; set; }
    public int? AssignedUserID { get; set; }
    public virtual User AssignedUser { get; set; }
    public virtual Status Status { get; set; }
    public IEnumerable<SelectListItem> Users { get; set; }
}

public class BugAssignmentList
{
    public BugAssignmentList()
    {
        BugAssignments = new List<BugAssignment>();
    }
    public int BugAssignmentListID { get; set; }
    public string Name { get; set; }
    public List<BugAssignment> BugAssignments { get; set; }
}

编辑/视图模型细节

Edit/Details ViewModel

public class BugAssignmentListDetailsViewModel
{
    public int BugAssignmentListID { get; set; }
    public string Name { get; set;}
    public List<BugAssignment> BugAssignments { get; set; }
}

我在我的控制器以下两种方法:

I have the following two methods in my controller:

    public ActionResult Edit(int id)
    {
        var balData = db.BugAssignmentLists.Include(bn => bn.BugAssignments).ToList();
        //this is a reference to the bug assignment list object whose ID was passed in
        BugAssignmentList bugAssignmentList = balData.Where(b => b.BugAssignmentListID == id).FirstOrDefault();
        //loop through the bug assignment list and for each bug assignment
        foreach (BugAssignment b in bugAssignmentList.BugAssignments)
        {
            //create the select list for each BugAssignment object
            b.Users = users.Select(u => new SelectListItem
            {
                Value = u.UserID.ToString(),
                Text = u.FullName,
                Selected = u.UserID == b.AssignedUserID //<<THIS IS WHERE IT IS MESSING UP
            });

        }
        BugAssignmentListDetailsViewModel detailsVM = new BugAssignmentListDetailsViewModel
        {
            BugAssignmentListID = id
        };
        if (bugAssignmentList != null)
        {
            detailsVM.BugAssignments = bugAssignmentList.BugAssignments;
            detailsVM.Name = bugAssignmentList.Name;
        }
        return View(detailsVM);
    }

    // POST
    [HttpPost]
    public ActionResult Edit(BugAssignmentListDetailsViewModel viewModel)
    {
        var balData = db.BugAssignmentLists.Include(bn => bn.BugAssignments).ToList();
        var users = db.Users.ToList();
        BugAssignmentList bugAssignmentList = balData.Where(b => b.BugAssignmentListID == viewModel.BugAssignmentListID).FirstOrDefault();
        bugAssignmentList.Name = viewModel.Name;
        bugAssignmentList.BugAssignments = viewModel.BugAssignments;

        if (ModelState.IsValid)
        {
            UpdateModel(bugAssignmentList, "BugAssignmentList");
            db.Entry(bugAssignmentList).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(bugAssignmentList);
    }

查看:

<table>    
<thead>
    <th>
        Bug Number
    </th>
    <th>
        Assigned User
    </th>
</thead>
    @for(int i=0; i<Model.BugAssignments.Count(); i++)
    {
        <tr>
            <td style="text-align: center;">
                @Html.EditorFor(modelItem => Model.BugAssignments[i].BugNumber)
            </td>
            <td>
                @Html.DropDownListFor(modelItem => Model.BugAssignments[i].AssignedUserID, Model.BugAssignments[i].Users, "-- Select User --")
                @Html.HiddenFor(modelItem => Model.BugAssignments[i].BugAssignmentID)
            </td>
        </tr>
    }
</table>

开机自检工作正常。看在数据库中,每个BugAssignment内BugAssignmentList的BugAssignments集合中与来自在我编辑观点下拉框中正确AssignedUserID更新,所以它不是POST方法。

The POST is working fine. Looking in the database, each BugAssignment in the BugAssignments collection within BugAssignmentList is updating with the proper AssignedUserID from the dropdown box in my Edit view, so it's not the POST method.

问题是在编辑(INT ID)。发生了什么事很奇怪。我已经把一个断点右foreach循环之前,我看着bugList.BugAssignments内。所有AssignedUserID的是正确的,但在循环完成后,我再次检查列表,BugAssignment.Users所有SelectListItems具有相同的用户选择。

The problem is in the Edit(int id). What's happening is very odd. I've put a breakpoint right before the foreach loop and I looked inside of bugList.BugAssignments. All the AssignedUserID's are correct, but after the loop completes, and I inspect the list again, all the SelectListItems in BugAssignment.Users have the same User selected.

什么是错的以下code,将导致此?

What is wrong with the following code that would cause this?

    foreach (BugAssignment b in bugAssignmentList.BugAssignments)
    {
        //create the select list for each BugAssignment object
        b.Users = users.Select(u => new SelectListItem
        {
            Value = u.UserID.ToString(),
            Text = u.FullName,
            Selected = u.UserID == b.AssignedUserID //<<THIS IS WHERE IT IS MESSING UP
        });

    }

此外,作为一个侧面说明:我希望接口看起来像这样(这就是为什么我要选择列表要对BugAssignment模型,除非有更简单的方法,我不想写一个BugAssignment。控制器/视图我希望有人能够快速将用户分配到错误号码如下所示:

Also, as a side note: I want the interface to look something like this (which is why I need the SelectList to be on the BugAssignment model, unless there is an easier way. I don't want to write a BugAssignment controller/view I want someone to be able to quickly assign users to bug numbers like the following:

下面是另一张截图,更好地证明了这种情况:

Here is another screenshot that better shows what is happening:

推荐答案

我相信这是一个封闭的问题。尝试创建一个本地副本 b.assignedUserID

I believe this is a closure issue. Try creating a local copy of b.assignedUserID:

foreach (BugAssignment b in bugAssignmentList.BugAssignments)
{
    var assignedUserID = b.AssignedUserID;

    //create the select list for each BugAssignment object
    b.Users = users.Select(u => new SelectListItem
    {
        Value = u.UserID.ToString(),
        Text = u.FullName,
        Selected = (u.UserID == assignedUserID),
    });

}

这篇关于在设置中选择的SelectList属性在ASP.NET的基础上选定的ID MVC 3中收集的每个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆