传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [System.Object]',但此字典需要类型为的模型项... [英] 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...

查看:187
本文介绍了传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [System.Object]',但此字典需要类型为的模型项...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的存储过程正在改变号码。列(字段)动态根据通过dropdownlist传递给它的参数....因为我是MVC的新手请帮忙...在从GetList(String MatchTypeName)重新调整结果后出现此错误.... 。错误:传入字典的模型项的类型为'System.Collections.Generic.List`1 [System.Object]',但此字典需要类型为'System.Collections.Generic.IEnumerable`1的模型项[ LearningMVC.Controllers.MyController]。问题在于cshtml代码 @model IEnumerable(LearningMVC.User)





public ActionResult Index(string MatchTypeName )

{



foreach(db.MatchTypeMasters中的MatchTypeMaster MatchType)

{

SelectListItem selectMatchTypes = new SelectListItem

{

Text = MatchType.MatchTypeName,

Value = MatchType.MatchTypeID,

$ / b $ b};



selectMatchTypeList.Add(selectMatchTypes);

ViewBag.MatchTypeName = selectMatchTypeList; < br $>
}



if(string.IsNullOrEmpty(MatchTypeName))

{

MatchTypeName =XYZ;

var bugedlist = GetList(MatchTypeName);

return View(bugedlist);

}

其他

{

var GridFill = GetList(MatchTypeName);

返回View(GridFill);

}



返回查看();



}



public List GetList(String MatchTypeName)

{

// var modelList = new List();

var result = new List();



using(SqlConnection conn = new SqlConnection(@Server = server_name; UID = XYZ; PWD =通过; database = db_name))

{

试试

{



conn .Open();



SqlCommand dCmd = new SqlCommand(SP_WEBBASEDREPorTS,conn);



dCmd.CommandType = CommandType.StoredProcedure;



dCmd.Parameters.Add(new SqlParameter(@ MatchType,MatchTypeName));



dCmd.Parameters.Add(新的SqlParameter(@ ModeFlag,击球行业平均值));



SqlDataAdapter da = new SqlDataAdapter(dCmd);

DataSet ds = new DataSet();

DataTable table = new DataTable();

ds.Clear() ;



da.Fill(table);

conn.Close();



foreach(table.Rows中的DataRow行)

{

var obj =(IDictionary< string,>)new ExpandoObject();

foreach(Table.Columns中的DataColumn col)

{

obj.Add(col.ColumnName,row [col.ColumnName]);

}

result.Add(obj);
}

}

catch

{



}

}



返回结果;





---------------------------------------------- -------------------------

CSHTML代码是:



@using GridMvc.Html;



@model IEnumerable(LearningMVC.User)

@ {

ViewBag.Title =索引;

布局=〜/ Views / Shared / _Layout.cshtml;

}





@ *

@ Html.ActionLink(新建,创建)

* @







@using(@ Html.BeginForm(Index,我的,FormMethod.Get))

{



@ Html.DropDownList(MatchTypeName,全选);

< input type =submitvalue =过滤器/>

}





@ Html.Grid(Model).AutoGenerateColumns()。Sortable(true)

解决方案

您应该将在actionresult中返回的集合类型更改为

 IEnumerable< user>或列表< user> < /  用户 >  < /  用户 >  



希望这有助于


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)

解决方案

You should change the type of collection you return in the actionresult to

IEnumerable<user> or List<user></user></user>


Hope this helps


这篇关于传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [System.Object]',但此字典需要类型为的模型项...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆