asp.net – .net MVC将linq数据从控制器传递到视图
|
我试图从控制器传递数据到视图.我在网上搜索但找不到解决方案. 如果我这样做它有效: 控制器: var yyy = (from a in Connection.Db.Authorities select a) ; ViewBag.data = yyy; 视图: @foreach(var item in ViewBag.data)
{
@item.Value
}
但是以下代码不起作用: 控制器: var yyy = (from a in Connection.Db.Authorities select new {Value = a.Value,TypeCode = a.TypeCode,Return = Calculate(a.Return)}) ;
ViewBag.data = yyy;
视图: @foreach(var item in ViewBag.data)
{
@item.Value
}
它为视图文件提供“item不包含Value的定义”. 任何帮助都会很棒. 谢谢. -edited:更新了第二个控制器linq查询.并更正了第一个控制器linq查询. 解决方法这是因为你已经选择了Value,Value没有Value这样的属性.您应该更改控制器:var yyy =(来自Connection.Db.Authorities中的a选择a.Value);至 var yyy = (from a in Connection.Db.Authorities select a); 或者将视图更改为 @foreach(var item in ViewBag.data)
{
@item
}
////////////////////////////////////////////////编辑/ /////////////////////////////////////////////// public class AuthoritiesViewModel
{
public string Value { get; set; }
public string TypeCode { get; set; }
public string Return { get; set; }
}
并更改您的控制器: var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value,Return = Calculate(a.Return)});
ViewBag.data = yyy;
在您的视图中,您将能够使用: <table>
<tr>
<th>Value</th>
<th>TypeCode</th>
<th>Return</th>
</tr>
@foreach(AuthoritiesViewModel item in ViewBag.data)
{
<tr>
<td>@item.Value<td>
<td>@item.TypeCode<td>
<td>@item.Return<td>
</tr>
}
</table>
另外,我有一个问题要问你.为什么使用ViewBag将数据从控制器传递到视图?为什么不根据MVC模式使用Model传递这些数据? ////////////////////////////////////////////////更多编辑//////////////////////////////////////////////// public class AuthoritiesViewModel
{
public string Value { get; set; }
public string TypeCode { get; set; }
public string Return { get; set; }
}
public class AnotherQueryViewModel
{
public string AnotherQueryValue { get; set; }
public string AnotherQueryTypeCode { get; set; }
public string AnotherQueryReturn { get; set; }
}
public class ModelClass
{
IEnumerable<AuthoritiesViewModel> Authorities { get; set; }
IEnumerable<AnotherQueryViewModel> AnotherQueryResults { get; set; }
}
并更改控制器: var yyy = (from a in Connection.Db.Authorities select new AuthoritiesViewModel{ Value = a.Value,Return = Calculate(a.Return)});
// do your another select
var zzz = (from smthing select new AnotherQueryViewModel ...)
// create model instance
ModelClass model = new ModelClass()
{
Authorities = yyy.AsEnumerable(),AnotherQueryResults = zzz..AsEnumerable()
}
// return view with model
return View("view",model);
在视图中你可以使用: @model ModelClass
@*display first query result*@
<table>
<tr>
<th>Value</th>
<th>TypeCode</th>
<th>Return</th>
</tr>
@foreach(AuthoritiesViewModel item in Model.Authorities)
{
<tr>
<td>@item.Value<td>
<td>@item.TypeCode<td>
<td>@item.Return<td>
</tr>
}
</table>
@*display second query result*@
<table>
<tr>
<th>Another Query Value</th>
<th>Another Query TypeCode</th>
<th>Another Query Return</th>
</tr>
@foreach(AnotherQueryViewModel item in Model.AnotherQueryResults)
{
<tr>
<td>@item.AnotherQueryValue<td>
<td>@item.AnotherQueryTypeCode<td>
<td>@item.AnotherQueryReturn<td>
</tr>
}
</table> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 版本化ASP.NET Web应用程序
- asp.net-mvc – 如何在MVC WebGrid中显示行号
- asp.net – 如何在回发上保持变量
- asp.net-mvc – CKEditor和ASP.Net MVC 3 RequiredAttribut
- 如何缓存输出的action方法,返回图像到asp.net mvc中的视图?
- asp.net-mvc – ASP.Net MVC 3:反向授权属性
- ASP.NET C#5异步Web应用程序使用异步和等待
- 我可以在asp.net 4.0站点中托管.net 2.0虚拟目录吗?
- asp.net-mvc – 用户不在角色时的ASP.NET登录重定向循环
- asp.net – 如何将服务器时间转换为本地时间
- 部署 – Crystal报表文件未部署到ASP.NET MVC中的
- asp.net – 504服务器上的错误
- asp.net – SignalR不能与.Net Core一起使用
- asp.net – web配置错误:无法识别的属性’xmlns
- 从asp.net代码后面读表单认证cookie
- ASP.NET 2.0中的Gridview列宽
- ASP.NET Core中实现用户登录验证的最低配置示例代
- asp.net-mvc – asp.net mvc针对不同操作的不同验
- asp.net-mvc – 如果value为空,在razor模板上放置
- asp.net-mvc – Asp.Net MVC 2 Html.TextBoxFor为
