MVC 将 @Html.DropDownListFor 的选定值设置为查询字符串值
MVC Set Selected Value of @Html.DropDownListFor to Querystring value
我想我快到了,但出于某种原因,下拉列表默认为列表中的第一项,而不是我传递的值 (defaultChoice)。有什么想法吗?
@{
var defaultChoice = Request.QueryString["id"];
var choices = new SelectList(ViewBag.Choices, "myItemId", "myProductName", defaultChoice);
}
@Html.DropdownListFor(m =>m.myItemIdPrimary, choices)
最终代码但生成错误:
string defaultChoice = Request.QueryString["id"];
int idVal = int.Parse(defaultChoice);
var choices = new SelectList(ViewBag.Choices, "myItemId", "myProductName", idVal);
Model.myItemIdPrimary = idVal;
我收到错误: 值不能为空(在 int idVal = int.Parse(defualtChoice) 行)
您可以为模型赋值。
像这样
@{
var defaultChoice = Request.QueryString["id"];
var choices = new SelectList(ViewBag.Choices, "myItemId", "myProductName", defaultChoice);
Model.myItemIdPrimary=defaultChoice;
}
我想我快到了,但出于某种原因,下拉列表默认为列表中的第一项,而不是我传递的值 (defaultChoice)。有什么想法吗?
@{
var defaultChoice = Request.QueryString["id"];
var choices = new SelectList(ViewBag.Choices, "myItemId", "myProductName", defaultChoice);
}
@Html.DropdownListFor(m =>m.myItemIdPrimary, choices)
最终代码但生成错误:
string defaultChoice = Request.QueryString["id"];
int idVal = int.Parse(defaultChoice);
var choices = new SelectList(ViewBag.Choices, "myItemId", "myProductName", idVal);
Model.myItemIdPrimary = idVal;
我收到错误: 值不能为空(在 int idVal = int.Parse(defualtChoice) 行)
您可以为模型赋值。
像这样
@{
var defaultChoice = Request.QueryString["id"];
var choices = new SelectList(ViewBag.Choices, "myItemId", "myProductName", defaultChoice);
Model.myItemIdPrimary=defaultChoice;
}