我有一個視圖中的這個下拉列表,我用數據庫中的值填充。我結合一些值,並把它們對齊:MVC DropDownListFor不顯示空格添加到列表中的值
foreach (DataRow dr in dt.Rows)
{
string loc = dr.IsNull("STATEGEOG_LOC_NAME") ? "" : dr["STATEGEOG_LOC_NAME"].ToString();
if (loc.Length < maxloc)
loc = SetStringLength(loc, maxloc);
string name = dr.IsNull("NAME") ? "" : dr["NAME"].ToString();
if (name.Length < maxname)
name = SetStringLength(name, maxname);
string mtf = dr.IsNull("MTF_CODE") ? "" : dr["MTF_CODE"].ToString();
if (mtf.Length < maxmtf)
mtf = SetStringLength(mtf, maxmtf);
TextValuePair model = new TextValuePair()
{
Value = dr.IsNull("IEN") ? "" : dr["IEN"].ToString(),
Text = loc + " " + name + " " + mtf
};
countries.Add(model);
}
private static string SetStringLength(string input, int len)
{
return input.PadRight(len, ' ');
}
值I比較簡單地找到最長的值的數據庫,並墊空間的電流值,使之比長。我看到每一行是這樣的:
"MARYLAND 10 CSH (FT MEADE MD) A11B1 "
我把它發送到視圖與此:
[HttpGet]
public ActionResult GetAllGeoLocations()
{
return Json(Repository.GetGeoLocationList(), JsonRequestBehavior.AllowGet);
}
而且它加載到DDL是這樣的:
$.ajax({
type: 'GET',
dataType: 'json',
url: '@Url.Action("GetAllGeoLocations")',
success: function (data) {
$("#ddlLocation").empty();
$.each(data, function (Value, Text) {
var opt = '<option value=' + Text.Value + '>' + Text.Text + '</option>';
$("#ddlLocation").append(opt);
});
}
});
每個值我檢查了什麼時候我調試頁面顯示適當的填充。但是當下拉單擊時,填充消失。誰能告訴我爲什麼?
當我使用調試器在頁面中我看到的值加載到DDL是這樣的:
"<option value=6005>VIRGINIA 1 DENTAL SQ/SGD F1783 </option>"
但是下拉秀這樣的:
什麼是'maxLoc'?嘗試'&nbsp'來增加空間。 – mmushtaq