我使用的是Asp.net MVC。我想根据下拉列表选择值选择值。但我得到例外。这是我的代码 控制器
public ActionResult batsmenGrid()
{
List<batsmenModel> bM = new List<batsmenModel>();
// DataTable dtbl = new DataTable();
List<string> teams = new List<string>();
DataSet ds = new DataSet();
ds = Connection.DAL.GetQuery("select * from batsmen"); // Using DAL file saved in App_Code folder
foreach (DataRow dr in ds.Tables[0].Rows)
{
bM.Add(new batsmenModel
{
_bid = Convert.ToInt32(dr["b_id"].ToString()),
_bName = dr["b_name"].ToString(),
_bAverage = dr["b_average"].ToString(),
_team = dr["team"].ToString()
});
}
var itms = bM.GroupBy(g => new { g._team }).Select(s => new { name = s.Key._team, count = s.Count(), details = s.Select(ss => ss) }).ToList();
foreach (var v in itms)
{
teams.Add(v.name.ToString());
}
ViewBag.Teams = teams; //View pe data phenknay k lia use kiya ha
ViewBag.Team2 = new SelectList(teams.AsEnumerable()); // Another way to use dropdown list
return View(bM);
}
[HttpPost]
//[ActionName("GetbatsmenWithId")]
public ActionResult batsmenGrid(string MyDrpId1)
{
List<batsmenModel> bM = new List<batsmenModel>();
// DataTable dtbl = new DataTable();
List<String> teams = new List<String>();
DataSet ds = new DataSet();
ds = Connection.DAL.GetQuery("select * from batsmen where team = "+"'"+MyDrpId1+"'"); // Using DAL file saved in App_Code folder
foreach (DataRow dr in ds.Tables[0].Rows)
{
bM.Add(new batsmenModel
{
_bid = Convert.ToInt32(dr["b_id"].ToString()),
_bName = dr["b_name"].ToString(),
_bAverage = dr["b_average"].ToString(),
_team = dr["team"].ToString()
});
}
var itms = bM.GroupBy(g => new { g._team }).Select(s => new { name = s.Key._team, count = s.Count(), details = s.Select(ss => ss) }).ToList();
//var itms = bM.GroupBy(g => new { g._team }).Select(s => new { name = s.Key._team, count = s.Count(), details = s.Select(ss => ss) }).ToList();
foreach (var v in itms)
{
teams.Add(v.name.ToString());
}
ViewBag.Teams = teams;
return View(bM);
}
这是我的观点
@model IEnumerable<MvcApplication1.Models.batsmenModel>
@{
ViewBag.Title = "batsmenGrid";
WebGrid grid = new WebGrid(Model, rowsPerPage: 2);
WebGrid grd = new WebGrid();
}
@using (Html.BeginForm("batsmenGrid", "batsmen"))
{
@grid.GetHtml(
columns: new[]
{
grid.Column("_bid","Id"), //Models fields to display
grid.Column("_bName","Name"),
grid.Column("_bAverage","Average"),
grid.Column("_team","Team")
}
);
@Html.DropDownList("MyDrpId1",(SelectList)ViewBag.Team2,"---Select Team--- ");
<input type="submit" value="Get" />
}
当我打开i浏览器时,我在gridview中获取数据以及下拉但是当我选择值并提交时它会抛出异常 具有键“MyDrpId1”的ViewData项的类型为“System.String”,但必须是“IEnumerable”类型。