首页 > 解决方案 > DropDownListFor - 需要帮助定义“SelectedValue”属性

问题描述

我需要帮助了解如何SelectedValueHTML.DropDownListFor.

到目前为止,这是我所拥有的:在模型的另一部分,MAint是存储在数据库中的。

在模型中:

public IList<SelectListItem> SelectListMA(int MA)
    {
        
        IList < SelectListItem > List = new List<SelectListItem>();
        SelectListItem Item;
        for(int i = 1; i <= 9; i++)
        {
            Item = new SelectListItem
            {
                Value = i.ToString(),
                Text = i.ToString(),
                Selected = i == MA
            };
            List.Add(Item);
        }
        return List;
    }

在控制器中:

ViewBag.MA = _Repository.SelectListMA(5);

最后,在视图中:

@Html.DropDownListFor(model => model.MA, new SelectList(ViewBag.MA, dataValueField: "Value", dataTextField: "Text"))

在视图中,下拉菜单显示正常,但是没有设置默认值,我正在使用模型将原始模型传递给编辑,View因此使用ViewBag

编辑:请求更多细节 - 这是它所属的完整控制器。

    private readonly IRacesRepos _RaceRepository;

    public BaseTeamsController(IRacesRepos _Repository)
    {
        _RaceRepository = _Repository;


    }
    //sets the Model to the repository
    public BaseTeamsController() : this(new ModelRaces()) { }
        

    public ActionResult BEditPlayer(int ID)
        {
    
            Races_Players SelectedPlayer = _RaceRepository.GetPlayerBase(ID);
    
    
            ViewBag.MA = _RaceRepository.SelectListMA(SelectedPlayer.MA);
    
            ViewBag.ST = _RaceRepository.SelectListST(SelectedPlayer.ST);
            ViewBag.AG = _RaceRepository.SelectListAG(SelectedPlayer.AG);
            ViewBag.PA = _RaceRepository.SelectListPA((int)SelectedPlayer.PA);
            ViewBag.AV = _RaceRepository.SelectListAV(SelectedPlayer.AV);
            ViewBag.Race = _RaceRepository.GetRaceBase(SelectedPlayer.RaceID);
            return View(SelectedPlayer);
        }

我使用的是 MVC 版本 5,ModelRaces 是包含代码的模型的名称,MA 是数据库中模型中的一个 int。

全视图

@model BB2020MVC.Models.Races_Players

@{
    ViewBag.Title = "Add Player to " + ViewBag.RaceName;
}



@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
<div class="form-horizontal">
    <h4>Add Player to @ViewBag.RaceName</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.PlayerID)
    @Html.HiddenFor(model => model.RaceID)

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10 focus">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.MA, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.MA, new SelectList(ViewBag.MA, dataTextField: "Text", dataValueField:"Value") , new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.MA, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ST, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.ST, new SelectList(ViewBag.ST, dataTextField: "Text", dataValueField: "Value"), new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ST, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.AG, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.AG, new SelectList(ViewBag.AG, dataTextField: "Text", dataValueField: "Value"), new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.AG, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PA, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.PA, new SelectList(ViewBag.PA, dataTextField: "Text", dataValueField: "Value") ,new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PA, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.AV, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.AV, new SelectList(ViewBag.AV, dataTextField: "Text", dataValueField: "Value") ,new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.AV, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Cost, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Cost, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Cost, "", new { @class = "text-danger" })
        </div>
    </div>




    <div class="form-group">
        @Html.LabelFor(model => model.MaxQTY, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.MaxQTY, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.MaxQTY, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Cancel", "ViewRace", new { ID = Model.RaceID })
</div>

编辑:进行了一些测试和阅读,问题似乎是视图没有选择选定的值,model => model.MA但编辑时该值不为空,因此任何已被选择SelectListItem或被SelectList忽略的值。将任何值传递给selectedValue除数字或单词以外的任何值(例如“1”或“Word”,而不是 int 或字符串类型的变量)都会导致项目不被选中。不能解决问题,但很有趣。

标签: asp.net-mvchtml.dropdownlistfor

解决方案


试试这个语法:

在视图中

@Html.DropDownListFor(model => model.MA, ViewBag.MA,"Select One", new { htmlAttributes = new { @class = "form-control" } })

在存储库中:

public SelectList SelectListMA(int MA)
    {
   var List = new List<SelectListItem>();
       
        for(int i = 1; i <= 9; i++)
        {
           List.Add(new SelectListItem
            {
                Value = i.ToString(),
                Text = i.ToString(),
             });
           
        }
     return  new SelectList(list, "Value", "Text",MA);
}

但是您不需要将现有列表转换为 SelectListItem 列表。只需使用 retun new SelectList(yourlist, "yourvalueproperty", "yourtextproperty",);


推荐阅读