2015-05-04 88 views
1

我在MVC中填写了下拉列表,但工作正常,但现在我想用Dapper来做,但卡住了。如何在MVC-4中使用dapper填充下拉列表

的DropDownList在MVC没有小巧玲珑

控制器

[HttpPost] 
    public ActionResult Create(User ur) 
    { 
     string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True"; 
     SqlConnection con = new SqlConnection(str); 
     string query = "Insert into tblTest (Name,Email,MobileNo) values('" + ur.Name + "','" + ur.Email + "','" + ur.MobileNo + "')"; 
     con.Open(); 
     SqlCommand cmd = new SqlCommand(query, con); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     TempData["msg"] = "<script>alert('Inserted Successfully');</script>"; 
     ModelState.Clear(); 
     FillCountry(); 

    } 
    public void FillCountry() 
    { 
     string str = @"Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True"; 
     SqlConnection con = new SqlConnection(str); 
     string query = "select * from tbl_country "; 
     SqlCommand cmd = new SqlCommand(query, con); 
     con.Open(); 
     SqlDataReader rdr = cmd.ExecuteReader(); 
     List<SelectListItem> li = new List<SelectListItem>(); 
     li.Add(new SelectListItem { Text = "Select", Value = "0" }); 
     while (rdr.Read()) 
     { 
      li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() }); 
     } 

     ViewData["country"] = li; 

    } 

查看

@{ Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }); } 
@Html.DropDownList("country", ViewData["country"] as List<SelectListItem>, new {onchange = "this.form.submit();" }) 
@{ Html.EndForm(); } 

这就是我想现在要做的

的DropDownList在MVC用小巧精致的

型号

public class Region 
{ 
    private int _CountryId; 
    private string _CountryName; 

    public int CountryId 
    { 
     get { return _CountryId; } 
     set { _CountryId = value; } 
    } 


    public string CountryName 
    { 
     get { return _CountryName; } 
     set { _CountryName = value; } 
    } 

控制器

[HttpPost] 
    public ActionResult AddMobiles(TBMobileDetails MD, HttpPostedFileBase file) 
    { 
     FileUpload(file); 
     MobileMain MM = new MobileMain(); 
     MM.AddMobiles(MD); 
     FillCountry();   
     return RedirectToAction("AllMobileList"); 
    } 

滞留在这部分如何使用短小精悍补吗?如何填充我的列表?

public void FillCountry() 
    { 
     List<Region> li = new List<Region>(); 
     var para = new DynamicParameters(); 
     para.Add("@Type", 1); 
     var result = con.Query<Region>("Sp_MVCDapperDDl", para, commandType: CommandType.StoredProcedure); 
    } 

查看

@{ Html.BeginForm("AddMobiles", "AddMobile", FormMethod.Post, new { enctype = "multipart/form-data" }); } 
    @Html.DropDownList("country", ViewData["country"] as List<SelectListItem>, new { onchange = "this.form.submit();" }) 
    @{ Html.EndForm(); } 
+0

什么编译器时,你就可以悬停\ –

+0

@EhsanSajjad是infering''VAR result''的类型? – Navy

+0

是什么''con.Query返回类型'' –

回答

2

您传递IEnumerable<Region>类型的ViewData["country"]对象,而在查看要将它铸造IEnumerable<SelectListItem>,不会在动作变化FillCountry()明显的努力,使SelectList

public void FillCountry() 
    { 
     List<Region> li = new List<Region>(); 
     var para = new DynamicParameters(); 
     para.Add("@Type", 1); 
     var result = con.Query<Region>("Sp_MVCDapperDDl", para, commandType: CommandType.StoredProcedure); 
     var list = new SelectList(result,"CountryId","CountryName"); 
    } 

,并考虑现在将它转换为SelectList

@Html.DropDownList("country", ViewData["country"] as SelectList, new {onchange = "this.form.submit();" }) 

这将让你去。

+0

仍然得到同样的错误 – Navy

+0

@Navy你在''ViewData''把?你必须把''计算机[“国”] =列表;'' –

+0

是的,我正在通过相同的 – Navy