C# 在 View MVC 中访问控制器变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18488667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Controller variable in View MVC
提问by Inkey
I have started to use MVC and i have set up a simple project that has a controller than returns a value from entity FrameWork. I have a controller for the index page which visual studio setup as default in its template.
我已经开始使用 MVC 并且我已经建立了一个简单的项目,它有一个控制器而不是从实体 FrameWork 返回一个值。我有一个用于索引页面的控制器,Visual Studio 将其设置为模板中的默认值。
I am hoping that this value will be returned to the index page of the website.
我希望这个值会返回到网站的索引页面。
Controller code
控制器代码
MISEntities db = new MISEntities();
public ActionResult Index()
{
ViewBag.Message = "Real Time Production";
var scrap = from r in db.tbl_dppIT
select r.CastGood;
return View(scrap);
}
How do i access the var scrap using razor? I have looked at the viewbag method and other razor syntax but cant seem to find how i access this item.
如何使用剃刀访问 var 废料?我查看了 viewbag 方法和其他 razor 语法,但似乎无法找到我如何访问此项目。
采纳答案by fourpastmidnight
Just because the controller variable is declared using the var
keyword doesn't mean it doesn't have a type. Rather, this tells C# that the type should be inferred by its context (in this case, an IEnumerable<T>
of whatever type tbl_dppIT.CastGood
is).
仅仅因为使用var
关键字声明控制器变量并不意味着它没有类型。相反,这告诉 C# 类型应该由其上下文推断(在这种情况下,IEnumerable<T>
无论类型tbl_dppIT.CastGood
是什么)。
So, the other three answers are partly correct, you need to define the type of model being passed into the View via the razor keyword @model
as noted by the other answers:
因此,其他三个答案部分正确,您需要定义通过 razor 关键字传递到 View 的模型类型,@model
如其他答案所述:
// In your view Razor:
@model IEnumerable</* type of CastGood */>
There is an alternative to the three answers already specified, and that is sticking the scrap
variable into the ViewBag
in your controller:
已经指定的三个答案还有一个替代方案,那就是将scrap
变量粘贴到ViewBag
控制器中:
ViewBag.Scrap = scrap;
When you access the expando property Scrap
from the ViewBag
in the view, it will be a dynamic object, so you won't get the aid of IntelliSense. But, it is another way, just so that you know all the possibilities.
当您Scrap
从ViewBag
视图中访问 expando 属性时,它将是一个动态对象,因此您将无法获得 IntelliSense 的帮助。但是,这是另一种方式,只是为了让您知道所有的可能性。
UPDATE:
更新:
Based on your comments below, it looks like if CastGood is a database column that is allowed to be null and whose type is int
, then you'd want:
根据您在下面的评论,看起来如果 CastGood 是一个允许为 null 且其类型为 的数据库列int
,那么您需要:
@model IEnumerable<int?>
HTH.
哈。
回答by Dustin Kingen
You need to declare the type of the variable scrap as the model inside the View using the @model
keyword.
您需要使用@model
关键字将变量 scrap 的类型声明为 View 内的模型。
// View.cshtml
@model IEnumerable<Namespace.ScrapType>
@foreach(var item in Model) {
<p>@item.SomeProperty</p>
}
See Part 3: Views and ViewModelsof the MVC Music Store example.
回答by Ufuk Hac?o?ullar?
You need to strongly type your view and it will available as the Model instance.
您需要强输入您的视图,它将作为模型实例使用。
@model IEumerable<MyType>
@foreach(var item in Model)
{
@*do something with it *@
}