public ActionResult Index()
{
return View("test", new {a = 1, b = 2});
}
我的观点:
<%= a; %>
我得到一个错误:
'a' is not declared. It may be inaccessible due to its protection level.
最佳答案
您离解决方案很近了!只需添加模型即可访问其属性,如下所示
//in your view
<%= Model.a %>
但我想建议避免在 Controller 中使用匿名类型 return View(new{a=foo,b=bar})
不是一个好主意。
请按照以下步骤操作
创建模型
namespace ModelCentral{ public class AbModel { public int a{get;set;}; public int b{get;set;}; } }
然后像这样编辑你的 Controller Action
public ActionResult Index() { var model = new AbModel() {a = 1, b = 2}; return View("test",model); }
终于在你的视野中
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ModelCentral.AbModel>" %> <%= Model.a%>
关于c# - ASP.net 返回带有数据变量的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31068077/