本文介绍了如何将值传递给mvc中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器操作:

public ActionResult Index()

{

con = new SqlConnection(System.Configuration.ConfigurationManager。 ConnectionStrings [System.Data.SqlClient]。ToString());

con.Open();



DataSet ds = new DataSet();

SqlDataAdapter sda = new SqlDataAdapter(SELECT DISTINCT ReportID FROM ebRLMaster,con);

ViewBag.ReportId = cmd;

sda.TableMappings.Add(表,ebRLMaster);

sda.Fill(ds);

ViewBag.ReportId = ds;

ViewBag.ReportId =ebRLMaster.ReportID;

con.Close();

返回视图(RLMaster);

}

这是我视图中的下拉列表:

@ Html.DropDownList(ReportId,新的SelectList(ViewBag.ReportId,ReportId,ReportId) ,)



它在Dropdown代码中抛出以下错误:

DataBinding:System.Char不包含名为ReportId的属性。



请帮助!!!!!

This is my controller action:
public ActionResult Index()
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["System.Data.SqlClient"].ToString());
con.Open();

DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("SELECT DISTINCT ReportID FROM ebRLMaster", con);
ViewBag.ReportId = cmd;
sda.TableMappings.Add("Table", "ebRLMaster");
sda.Fill(ds);
ViewBag.ReportId = ds;
ViewBag.ReportId = "ebRLMaster.ReportID";
con.Close();
return View("RLMaster");
}
This is the dropdown in my view:
@Html.DropDownList("ReportId", new SelectList(ViewBag.ReportId, "ReportId", "ReportId"), "")

It throws the following error at Dropdown code:
DataBinding: System.Char does not contain a property with the name ReportId.

Please help!!!!!

推荐答案

@Html.DropDownList("ReportId", new SelectList(Test(), "Value", "Text"), "Select Report")





然后尝试从以下代码将数据集设置为Selectlistitem:



public static List< SelectListItem>测试()

{

// Decalre Selectlistitem

var objselectList = new List< SelectListItem>();



//在这里设置listof value =数据集值列表

foreach(listofvalue中的var objitem)

{

objselectList.Add(new SelectListItem

{

Text = objitem.ReportID,

Value = Convert.ToString(objitem.objitem) ),

});

}

返回objselectList;

}



Then try to set dataset to Selectlistitem from below code that is:

public static List<SelectListItem> Test()
{
//Decalre Selectlistitem
var objselectList = new List<SelectListItem>();

//over here set listof value = list of your dataset value
foreach (var objitem in listofvalue)
{
objselectList.Add(new SelectListItem
{
Text = objitem.ReportID,
Value = Convert.ToString(objitem.objitem),
});
}
return objselectList;
}


这篇关于如何将值传递给mvc中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 09:17