我创建了一个控制器,该控制器接受查询字符串中的一个参数,该参数是整数,并且我有一个下拉列表,我想基于该参数值从列表中设置一个默认值,但它不起作用。我正在使用DropDownListFor
html帮助程序来创建列表。
这是我的控制人:
public ActionResult Index(int accountType)
{
List<SelectListItem> listItems = new List<SelectListItem>
{
new SelectListItem{Text = "Competitor",Value = "Competitor"},
new SelectListItem{Text = "Consultant",Value = "Consultant"},
new SelectListItem{Text = "Customer",Value = "Customer"},
new SelectListItem{Text = "Investor",Value = "Investor"},
new SelectListItem{Text = "Partner",Value = "Partner"},
new SelectListItem{Text = "Influencer",Value = "Influencer"},
new SelectListItem{Text = "Press",Value = "Press"},
new SelectListItem{Text = "Prospect",Value = "Prospect"},
new SelectListItem{Text = "Reseller",Value = "Reseller"},
new SelectListItem{Text = "Supplier",Value = "Supplier"},
new SelectListItem{Text = "Vendor",Value = "Vendor"},
new SelectListItem{Text = "Other",Value = "Other"}
};
Account account = new Account
{
Name = "khaled",
Email = "test",
AccountType = listItems[accountType].Text
};
ViewBag.items = listItems;
return View(account);
}
我的观点: @using System.Globalization @model Form_Task.Models.Account
<div class="form-group">
@Html.Label("accounttype", "Account Type")
@Html.DropDownListFor(Model=>Model.AccountType,(List<SelectListItem>)ViewBag.items,new{@class = "form-control", @disabled="disabled"})
</div>
我的模特:
public class Account
{
public string Name { get; set; }
public string Email { get; set; }
public string AccountType { get; set;}
}
如果我将accountType
更改为字符串并按如下所示创建模型,则效果很好:
Account account = new Account
{
Name = "khaled",
Email = "test",
AccountType = accountType
};
答案 0 :(得分:0)
从您的评论看来,您似乎想根据其在列表中的位置来设置选定的值...如果必须这样做,那么在将其传递到视图包之前,此代码将起作用。
Account account = new Account
{
Name = "khaled",
Email = "test",
AccountType = listItems[accountType].Text
};
listItems[accountType].Selected = true;//Add this line
ViewBag.items = listItems;
return View(account);
但是,这不是一个好的处理方法,如果顺序发生变化会怎样?如果从查询字符串中获得的int
的长度大于listItems
的长度,会发生什么?
更典型的方法是根据selected
的值来设置SelectListItem
,如下所示:
public ActionResult Index(string accountType)//pass through the listitems value instead of its position in the list.
{
List<SelectListItem> listItems = new List<SelectListItem>
{
new SelectListItem{Text = "Competitor",Value = "Competitor"},
new SelectListItem{Text = "Consultant",Value = "Consultant"},
new SelectListItem{Text = "Customer",Value = "Customer"},
new SelectListItem{Text = "Investor",Value = "Investor"},
new SelectListItem{Text = "Partner",Value = "Partner"},
new SelectListItem{Text = "Influencer",Value = "Influencer"},
new SelectListItem{Text = "Press",Value = "Press"},
new SelectListItem{Text = "Prospect",Value = "Prospect"},
new SelectListItem{Text = "Reseller",Value = "Reseller"},
new SelectListItem{Text = "Supplier",Value = "Supplier"},
new SelectListItem{Text = "Vendor",Value = "Vendor"},
new SelectListItem{Text = "Other",Value = "Other"}
};
//loop through and check for a match
foreach (var item in listItems)
{
item.Selected = item.Value == accountType;
}
Account account = new Account
{
Name = "khaled",
Email = "test",
AccountType = listItems[accountType].Text
};
ViewBag.items = listItems;
return View(account);
}
这样,如果值与查询字符串不匹配,则不会引起异常,并且您的方法仍将完成。
编辑-刚注意到您正在使用@Html.DropDownListFor()
,请将其更改为@Html.DropDownList()
或将其更改为html5控件,否则Selected
属性将被忽略。
<div class="form-group">
@Html.Label("accounttype", "Account Type")
<select name="AccountType" class="form-control" disabled="disabled">
@foreach(var item in (List<SelectListItem>)ViewBag.items)
{
<option value='@item.Value' @(item.Selected ? "selected" : "")>@item.Text</option>
}
</select>
</div>