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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:22:36  来源:igfitidea点击:

Accessing Controller variable in View MVC

c#asp.net-mvcasp.net-mvc-4razor

提问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 varkeyword 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.CastGoodis).

仅仅因为使用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 @modelas 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 scrapvariable into the ViewBagin your controller:

已经指定的三个答案还有一个替代方案,那就是将scrap变量粘贴到ViewBag控制器中:

ViewBag.Scrap = scrap;

When you access the expando property Scrapfrom the ViewBagin 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.

当您ScrapViewBag视图中访问 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 @modelkeyword.

您需要使用@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.

请参阅第 3 部分:MVC 音乐商店示例的视图和视图模型

回答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 *@
}