Когда я динамически создаю список объектов в mvc 5, а затем добавляю новые объекты в этот список в контроллере, тогда он не будет создавать новый список объектов? - PullRequest
0 голосов
/ 10 июля 2020

В InMemoryRestaurantData.cs

using OdeToFood.Data.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
    
namespace OdeToFood.Data.Services
    {
        public class InMemoryRestaurantData : IRestaurantData
        {
    
            List<Restaurant> restaurants;
    
            public InMemoryRestaurantData()
            {
                restaurants = new List<Restaurant>
                {
                    new Restaurant{Id=1, Name= "Pizza", Cuisine = CuisineType.Italian},
                     new Restaurant{Id=2, Name= "Biryani", Cuisine = CuisineType.Indian},
                      new Restaurant{Id=3, Name= "French Fries", Cuisine = CuisineType.French},
    
    
                };
    
    
            }
    
            public void Add(Restaurant restaurant)
            {
                restaurants.Add(restaurant);
                restaurant.Id = restaurants.Max(r => r.Id) + 1;
    
            }
    
            public Restaurant Get(int id)
            {
                return restaurants.FirstOrDefault(r => r.Id == id);
            }
            public IEnumerable<Restaurant> GetAll()
            {
                return restaurants.OrderBy(r => r.Name);
                
    
               
            }
        }
    }

Когда я создаю список ресторанов динамически в InMemoryRestaurantData.cs, ​​а затем он выдает результат в localhost: // someportno // Рестораны, показывающие подробную информацию о ресторане ..

В RestaurantController.cs

using OdeToFood.Data.Models;
using OdeToFood.Data.Services;
   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace OdeToFood.Web.Controllers
    {
        public class RestaurantsController : Controller
        {
            private readonly IRestaurantData db;
    
            public RestaurantsController()
            {
                db = new InMemoryRestaurantData();
    
            }
            // GET: Restaurants
    
            [HttpGet]
            public ActionResult Index()
            {
                var model = db.GetAll();
                return View(model);
            }
    
            [HttpGet]
            public ActionResult Details(int id)
            {
                var model = db.Get(id);
                if(model == null)
                {
                    return View("Not Found");
                }
                return View(model);
    
            }
    
            [HttpGet]
            public ActionResult Create()
            {
                return View();
    
    
            }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(Restaurant restaurant)
            {
                db.Add(restaurant);
                return View();
            }
    
    
        }
 }

А вот и страница RestaurantController.cs. Когда я добавляю новые записи, нажимая на гиперссылку создания представления, а затем, когда я создаю новые записи, затем снова go назад к списку на странице local: // someportno // Restaurants, затем записи вставляются в список ..

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...