下拉列表 - 需要帮助定义"SelectedValue"属性



我需要帮助理解如何在HTML.DropDownListFor中设置SelectedValue属性。

这是我到目前为止所做的:在模型的另一部分,MA是存储在数据库中的int

在模型中:

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

最后,在View中:

@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的选定值,但在编辑时值不是空的,因此SelectListItemSelectList选择的任何值都被忽略。还可以向selectedValue传递除数字或单词以外的任何值(例如"1")或"word"(不是int或字符串类型的变量)导致项未被选中。这并不能解决问题,但却是一个有趣的观点。

试试这个语法:

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

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

这个问题的答案就在键盘和屏幕之间。

让我解释一下:

在额外的测试中,我错误地将_RaceRepository.GetPlayerBase(ID)更改为另一个为记录生成新基的函数。(即兴突变测试!)

这意味着每次我试图编辑播放器时,我都将默认值"1传递给视图,并在提交时编辑一个从未存在过的模型。

在设置正确的函数后,我得到了正确的值,因此所选值的设置自行排序。

然而,我将感谢Sergey有一个看,并试图协助,并提供一个更好的方式来提供一个SelectList(一个更好的版本将是使用ViewModel,这将不得不用于另一部分)。

对于那些感兴趣的人,这里是解决这个问题的代码:

From the Model:

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

return new SelectList(List,"Value","Text");
}

来自控制器:

public ActionResult BEditPlayer(int ID)
{
Races_Players SelectedPlayer = _RaceRepository.GetPlayerBase(ID);
ViewBag.MASelect = _RaceRepository.SelectListMA();
ViewBag.STSelect = _RaceRepository.SelectListST();
ViewBag.AGSelect = _RaceRepository.SelectListAG();
ViewBag.PASelect = _RaceRepository.SelectListPA();
ViewBag.AVSelect = _RaceRepository.SelectListAV();

return View(SelectedPlayer);
}

From the View: - as SelectList是用来在代码构建时阻止错误发生

@Html.DropDownListFor(model => model.MA, ViewBag.MASelect as SelectList, "Select One") 

同样,对于任何新的程序员,根据研究和测试,SelectListSelectListItem分别有SelectedValueSelected来选择默认值。

SelectedValue可以是标识列表的主键或列表的新默认值的任何对象(例如,列表中已经存在的值为整数,或默认值为null的字符串)
Selected是一个布尔值(即true或false),用于选择列表的值。你只需要使用其中一个。

请记住,如果该值作为model => model.[Variable]中的值从控制器传递给视图,则该值将被设置为传递的值,而不是SelectedValueSelected设置的值。

最新更新