2013-01-23 28 views
0

我有一个AJAX功能,正常工作如何使用下拉列表使用剃刀和Ajax

$.ajax({ 
       type: "POST", 
       url: urlemp, 
       success: function (returndata) { 
        if (returndata.ok) { 
        // var data = eval("" + returndata.data + ""); 
         select.empty(); 
         $.each(returndata.data, function (rec) { 

          select.append($('<option>' + returndata.data[rec].Name + '</option>')); 
         }); 
         select.show('slow'); 
         select.change(); 
        } 
        else { 
         window.alert(' error : ' + returndata.message); 
        } 

       } 
      } 
     ); 

,并在视图中我用这个选择器代码

<select id="cmbDept" onload="javascript:cascadingdropdown()"> 
     </select> 

现在我想改变此选择器的代码为剃须刀类型,如@ Html.xxxx 我该怎么做?

回答

1

尝试这样:

public class NamesViewModel 
    { 
    public string SelectedName { get; set; } 
    public IEnumerable<SelectListItem> Name { get; set; } 
    } 

控制器:

public class HomeController : Controller 
    { 
    public ActionResult Index() 
    { 
     var model = new NamesViewModel(); 
     // TODO: obviously those will come from your database 
     model.Names = new[] 
     { 
     new SelectListItem { Value = "1", Text = "Mary" }, 
     new SelectListItem { Value = "2", Text = "Joan" }, 
     new SelectListItem { Value = "3", Text = "Lisa" }, 
     }; 

     // Preselect the option with Value = "Mary" 
     // Make sure you have such option in the NAmes list 
     model.SelectedName = "Mary"; 
     return View(model); 
     } 
     } 

    View (~/Views/Home/Index.cshtml): 

    @model NamesViewModel 

    @Html.DropDownListFor(
     x => x.SelectedName, 
     Model.Name, 
     "Select Name" 
    )