简体   繁体   English

将数据传递到ASP.NET MVC5中的Razor View

[英]Passing data to Razor View in ASP.NET MVC5

I'm trying to learn ASP.NET and trying some basic things like passing hard-coded data to Razor views. 我正在尝试学习ASP.NET并尝试一些基本的东西,比如将硬编码数据传递给Razor视图。

I have a simple class in a model: 我在模型中有一个简单的类:

public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

In my CustomersController, I have this code: Basically, I'm just making an enumerable list of hard-coded customers, doing a simple get method and calling that method in the get route at /customers 在我的CustomersController中,我有这样的代码:基本上,我只是制作一个可枚举的硬编码客户列表,做一个简单的get方法并在/ customers的get路由中调用该方法

public class CustomersController : Controller
    {
        // GET: Customers
        public ActionResult Index()
        {
            var customers = GetCustomers();

            return View(customers);
        }

        public IEnumerable<Customer> GetCustomers()
        {
            return new List<Customer>
            {
                new Customer
                {
                    Id = 1,
                    Name = "Peter Parker"
                },

                new Customer
                {
                    Id = 2,
                    Name = "Eddie Brock"
                },

                new Customer
                {
                    Id = 3,
                    Name = "Mary Jane"
                },
            };
        }
    }
}

In my Razor view, I bring in the model and try to loop through the customers and print them in a list. 在我的Razor视图中,我引入模型并尝试遍历客户并将其打印在列表中。 Not sure what I'm doing wrong as the intellense won't bring up anything. 不知道我做错了什么,因为intellense不会带来任何东西。 I can't say like @customer.Name in the li.And if I hover over the Model.Customer in the @foreach block, it says that there's no definition of customer. 我不能说像li中的@ customer.Name。如果我将鼠标悬停在@foreach块中的Model.Customer上,它表示没有客户的定义。

@model Vidly.Models.Customer

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<ul>
    @foreach(var customer in Model.Customers)
    {
        <li>@customer</li>
    }
</ul>

The GetCustomers() method returns a collection of Customer objects and you are passing that collection to the view. GetCustomers()方法返回Customer对象的集合 ,并将该集合传递给视图。 So your view should be strongly typed to the collection Customer objects. 因此,您的视图应该强烈地键入到Customer对象集合中。 Your current code says it expects a single Customer object. 您当前的代码表示它需要一个Customer对象。

Change it to be strongly typed to a collection of Customer objects, and then inside your view, you may loop the Model , which is a collection ( you can iterate through a collection ). 将其更改为强类型为Customer对象的集合,然后在视图中,您可以循环Model ,这是一个集合( 您可以遍历集合 )。

@model List<Vidly.Models.Customer>    
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<ul>
    @foreach(var customer in Model)
    {
        <li>@customer.Id</li>
        <li>@customer.Name</li>
    }
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM