my stored procedure is changing the no. of columns(fields) dynamically according to the parameter passed to it through dropdownlist.... As I am new in MVC Please help in same... This error is coming after retuning the result from GetList(String MatchTypeName)..... Error: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.Object]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[LearningMVC.Controllers.MyController]'. Problem is with cshtml code @model IEnumerable(LearningMVC.User)
public ActionResult Index(string MatchTypeName)
{
foreach (MatchTypeMaster MatchType in db.MatchTypeMasters)
{
SelectListItem selectMatchTypes = new SelectListItem
{
Text = MatchType.MatchTypeName,
Value = MatchType.MatchTypeID,
};
selectMatchTypeList.Add(selectMatchTypes);
ViewBag.MatchTypeName = selectMatchTypeList;
}
if (string.IsNullOrEmpty(MatchTypeName))
{
MatchTypeName = "XYZ";
var bugedlist = GetList(MatchTypeName);
return View(bugedlist);
}
else
{
var GridFill = GetList(MatchTypeName);
return View(GridFill);
}
return View();
}
public List GetList(String MatchTypeName)
{
//var modelList = new List();
var result = new List();
using (SqlConnection conn = new SqlConnection(@"Server= server_name; Uid=xyz;pwd=pass; database=db_name"))
{
try
{
conn.Open();
SqlCommand dCmd = new SqlCommand("SP_WEBBASEDREPorTS", conn);
dCmd.CommandType = CommandType.StoredProcedure;
dCmd.Parameters.Add(new SqlParameter("@MatchType", MatchTypeName));
dCmd.Parameters.Add(new SqlParameter("@ModeFlag", "Batting Industry Average"));
SqlDataAdapter da = new SqlDataAdapter(dCmd);
DataSet ds = new DataSet();
DataTable table = new DataTable();
ds.Clear();
da.Fill(table);
conn.Close();
foreach (DataRow row in table.Rows)
{
var obj = (IDictionary<string,>)new ExpandoObject();
foreach (DataColumn col in table.Columns)
{
obj.Add(col.ColumnName, row[col.ColumnName]);
}
result.Add(obj);
}
}
catch
{
}
}
return result;
-----------------------------------------------------------------------
CSHTML Code is:
@using GridMvc.Html;
@model IEnumerable(LearningMVC.User)
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@*
@Html.ActionLink("Create New", "Create")
*@
@using (@Html.BeginForm("Index", "My", FormMethod.Get))
{
@Html.DropDownList("MatchTypeName", "Select All");
<input type="submit" value="Filter" />
}
@Html.Grid(Model).AutoGenerateColumns().Sortable(true)