我有一个视图中的这个下拉列表,我用数据库中的值填充。我结合一些值,并把它们对齐: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