DropDownList初始值重复 - MVC 5

时间:2014-06-11 18:13:50

标签: drop-down-menu asp.net-mvc-5

我的视图中有@foreach表格。每行中有两个项td。当我单击“编辑”按钮时,连续的可见项目将消失(DislayFor)并显示行中的隐藏项目(DropDownList

查看代码

        <td class="col-md-3">
            <span class="item-display">
                <span style="font-size: 17px">
                    @Html.DisplayFor(modelItem => item.Movie.Name)
                </span>
            </span>
            <span class="item-field">
                @Html.DropDownList("movieID", item.Movie.Name)
            </span>
        </td>

通过这样做,我可以在DropDownList中选择一个新值,然后将该更改保存到我的数据库中(然后隐藏DropDownList并取消隐藏DisplayFor。

一切正常,但我对初始选择的值有问题,它出现两次,初始值的实际value为0(与数据库中的任何内容无关)。

图片示例 enter image description here

问题:

现在我的下拉列表在点击编辑后添加一个值,最初选择的项目具有正确的名称,但索引为0(对我的数据库无效)。

我希望不添加最初选择的项目,而是将下拉列表的选择器设置为相应项目的CORRECT INDEX。我不确定为什么它会重复我选择的项目两次。

控制器代码

public ActionResult Index(string Filter, string searchString)
{

    if (String.IsNullOrEmpty(searchString) || String.IsNullOrEmpty(Filter) || (Int32.Parse(Filter) == 0))
    {
        ViewBag.employeeID = new SelectList(db.Employees, "ID", "Name", );
        ViewBag.movieID = new SelectList(db.Movies, "ID", "Name", initiallySelectedValue);
        ViewBag.roleID = new SelectList(db.Roles, "ID", "RoleType");
        var movieemployees = db.MovieEmployees.Include(m => m.Employee).Include(m => m.Movie).Include(m => m.Role);
        return View(movieemployees.ToList().OrderBy(x => x.Employee.Name));
    }
    else
    {
        ViewBag.employeeID = new SelectList(db.Employees, "ID", "Name");
        ViewBag.movieID = new SelectList(db.Movies, "ID", "Name");
        ViewBag.roleID = new SelectList(db.Roles, "ID", "RoleType");



        var parameter = Int32.Parse(Filter);
        return View(db.MovieEmployees.Include(m => m.Employee).Include(m => m.Movie).Include(m => m.Role).Where(x => (parameter == 1 && x.Movie.Name.Contains(searchString)) || (parameter == 2 && x.Employee.Name.Contains(searchString)) || (parameter == 3 && x.Role.RoleType.Contains(searchString))).Distinct().ToList().OrderBy(x => x.Employee.Name));
    }

}

2 个答案:

答案 0 :(得分:2)

您对DropDownList参数的理解不太正确,但您已经接近了! DropDownList(在您的情况下为item.Movie.Name)的第二个参数是添加option标签。如果你用一个硬编码的字符串替换它,它将作为它正在做的事情的一个很好的例子(你会看到该字符串作为每个选择输入的第一个选项)。

听起来我想要删除最后一个参数,因为它最终只能作为副本使用。你的代码看起来像这样:

@Html.DropDownList("movieID")

代码的重要部分是您使用密钥ViewData构建要存储在movieID中的对象的位置。你没有发布你的控制器代码,但我想它看起来像:

var movies = movieRepository.GetAllMovies();
ViewData["movieID"] = new SelectList(movies, "Name", "Id", initiallySelectedValue);

其中NameId是电影对象上属性的名称,而initiallySelectedValue则相当自我解释。

修改

以下是我将如何解决您的问题的示例:

<强>控制器

public ActionResult Index() {
    //Get all the possible movies that can be selected
    var movies = movieRepository.GetAllMovies();
    //Get a list of employees with their related favorite movie record
    var employeesWithFavoriteMovie = movieRepository.GetEmployeesWithMovie();

    var employeeModels = new List<EmployeeModel>();

    //Iterate through the list of employees and their favorite movie, and build the model
    foreach (var employeeWithFavoriteMovie in employeesWithFavoriteMovie) {
        employeeModels.Add(new EmployeeModel() {
            FirstName = employeeWithFavoriteMovie.FirstName,
            FavoriteMovieId = employeeWithFavoriteMovie.Movie.Id,
            MovieSelectList = new SelectList(movies, "Name", "Id", employeeWithFavoriteMovie.Movie.Id)
        });
    }

    return View(employeeModels);
}

查看

@model IEnumerable<WebApplication1.Controllers.EmployeeModel>

@foreach (var employeeModel in Model) {
    @Html.DropDownList("Test", employeeModel.MovieSelectList)
}

注意如何为每位员工构建SelectList,然后使用该员工当前最喜欢的电影ID填充每个列表。现在,这将使您能够为每位员工正确构建SelectList

答案 1 :(得分:0)

@Html.DropDownListFor(model => model.classmasterd_HCF[i].TD_TEACHER, new SelectList(Model.Teacher, "ParamKey", "ParamValue", Model.classmasterd_HCF[i].TD_TEACHER) as SelectList, new { @class = "form-control input-sm DtlField EditableCtrl", @style = "min-width:100%;", @disabled = "disabled" })

其中Model.Teacher中的Teacher是一个模型,在paramKey中具有代码,在paramvalue中具有描述。所选值保存在TD_TEACHER字段中