我正在獲取模型中的用戶資料并使用 SelectList,獲取帶有名稱和 ID 的國家/地區詳細資訊。
ID Name
1 USA
2 UK
3 CANADA
在我的頁面上,我有 TEXTBOX,其中顯示了從模型中獲取的國家名稱 USA。現在我想在我的下拉串列中選擇相同的國家名稱。
var GetNewCountry = new SelectList(_manualGridService.GetCountry().OrderBy(l => l.Name)
.ToDictionary(us => us.ID, us => us.Name), "Key", "Value");
ViewBag.GetNewCountry = GetNewCountry ;
這是我的視圖下拉代碼
<div class="form-group">
<select asp-for="ConId" asp-items="@ViewBag.GetNewCountry" class="form-control">
<option value=""> Select </option>
</select>
<span asp-validation-for="ConId" class="text-danger"></span>
</div>
引數詳情
public int ConId { get; set; }
這是我的帶引數的文本框代碼
public string UserConName{ get; set; }
<div class="form-group">
<input asp-for="UserConName" class="form-control" readonly="readonly" autocomplete="off" placeholder="Country Name" style="width:107%" />
</div>
UserConName文本框包含值USA,我也想在我的 DROPDOWN LIST中保留選中的USA 。
我的解決方案
string strId = "";
foreach (var item in GetNewCountry )
{
if (item.Text == res.ConName)
{
strId = item.Value;
}
}
if (!string.IsNullOrEmpty(strId))
{
res.ConId= int.Parse(strId);
}
uj5u.com熱心網友回復:
縮短答案
var GetNewCountry = new List<SelectListItem>();
foreach (var item in _manualGridService.GetCountry().OrderBy(l => l.Name).ToDictionary(us => us.ID, us => us.Name))
{
GetNewCountry.Add(new SelectListItem() {
Value = item.Key.ToString(),
Text = item.Value,
Selected = item.Value == "USA" ? true : false
});
}
ViewBag.GetNewCountry = GetNewCountry ;
詳細解釋
首先你需要知道,通過使用new SelectList(_manualGridService.GetCountry().OrderBy(l => l.Name) .ToDictionary(us => us.ID, us => us.Name), "Key", "Value"),Keyequals toId的值,Valueequals toName的值。
再對比一下這個建構式的定義:public SelectList(IEnumerable items, string dataValueField, string dataTextField),可以知道ID代表<option>'值','名稱'代表<option>'文本。
<option value="IDValue">NameValue</option>
最后SelectList包含另一個可以設定下拉串列選擇值的建構式:
public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
ID最重要的是,在您的場景中,選擇的值與,它匹配的值不匹配Name,因此您需要更改SelectList如下:
var GetNewCountry = new SelectList(_manualGridService.GetCountry().OrderBy(l => l.Name)
.ToDictionary(us => us.ID, us => us.Name), "Value", "Value","USA");
生成的html:
<select class="form-control" data-val="true" data-val-required="The ConId field is required." id="ConId" name="ConId">
<option value=""> Select </option>
<option selected="selected" value="NameValue1">NameValue1</option>
<option value="NameValue2">NameValue2</option>
//...
</select>
筆記:
這樣你就可以得到value="USA"選項被選中,但是另一個問題,你<select asp-for="ConId">這里的select系結了值ConId,但是選項值現在代表了Name,所以如果你做表單提交它不能系結成功。
因此,如果您不想更改為<select asp-for="ConId">,則<select asp-for="UserConName">需要修改后端代碼,如下所示:
var GetNewCountry = new List<SelectListItem>();
foreach (var item in _manualGridService.GetCountry().OrderBy(l => l.Name).ToDictionary(us => us.ID, us => us.Name))
{
GetNewCountry.Add(new SelectListItem() { Value = item.Key.ToString(), Text = item.Value, Selected = item.Value == "USA" ? true : false });
}
ViewBag.GetNewCountry = GetNewCountry ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485187.html
標籤:asp.net-mvc asp.net 核心 模型视图控制器
