Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
Posted
Updated 29-Aug-13 0:42am
v2
Comments
Jameel VM 29-Aug-13 9:44am    
Please format the question properly

1 solution

You should change the type of collection you return in the actionresult to
C#
IEnumerable<user> or List<user></user></user>

Hope this helps
 
Share this answer
 
Comments
Ruter11 29-Aug-13 12:21pm    
Thanks for reply but I want dynamic column as column are generating according to parameter passed that means no. of col and name of column .......... that's why I took List<<dynamic>>......... if you have any other solution to create auto generated columns in MVC please let me know

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900