2017-01-11 21 views
0

我有一個視圖中的這個下拉列表,我用數據庫中的值填充。我結合一些值,並把它們對齊: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>" 

但是下拉秀這樣的:

enter image description here

+0

什麼是'maxLoc'?嘗試'&nbsp'來增加空間。 – mmushtaq

回答

1

不要怪MVC /剃刀爲此。 這是瀏覽器。 嘗試用

"&nbsp;" 

嘗試更換你的期權價值空間的使用方法:

var opt = '<option value=' + Text.Value + '>' + Text.Text.replace(/\s/g, "&nbsp;") + '</option>'; 

在$。每次看。

這將強制空格。

+0

這也行不通,我用var text = Text.Text.replace(「」,「 」),我得到的是字符串中的一個 。 –

+0

是的,我的錯。更新了答案! – FaustsErbe

+0

把它放置在頁面中? –