【问题标题】:How to show data from SQL database in _Layout.cshtml in MVC如何在 MVC 中的 _Layout.cshtml 中显示 SQL 数据库中的数据
【发布时间】:2017-07-18 07:39:44
【问题描述】:

我正在处理一个MVC OnlineShop 项目,我在SQL server 中创建了类别和产品数据库,现在我想在主页上显示此数据库中的类别的网络。我在创建 session 并在 _Layout.cshtml 中调用此 session 之前尝试过,但我发现 session 不是显示类别的好主意虽然它有效,但在主页上,因为会话主要用于 cookie ...... 问题:这是正确的做法,还是有更好的办法? 我在我的Homecontroller.cs 中写了这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOnlineShop.Models;

namespace MVCOnlineShop.Controllers
{
    public class HomeController : Controller
    {
        OnlineStoreEntities storeDB = new OnlineStoreEntities();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var Categories = storeDB.Categories.ToList();
            return View(Categories);
        }
        //
        // GET: /Home/Browse
        public ActionResult Browse(string Category)
        {
            // Retrieve Category and its Associated Products from database
            var CategoryModel = storeDB.Categories.Include("Products")
                .Single(g => g.CategoryName == Category);

            return View(CategoryModel);
        }
        //
        // GET: /Home/Details
        public ActionResult Details(int id)
        {
            var Product = storeDB.Products.Find(id);

            return View(Product);
        }
        //
        // GET: /Home/Browse?Category=Games

    }
}

并在Views/Home 中创建了一个PartialView,名为CategoryLayout.cshtml

@model IEnumerable<MVCOnlineShop.Models.Category>

@{
    ViewBag.Title = "Categories";
}
<ul>
    @foreach (var Category in Model)
    {

        <li>
            @Html.ActionLink(Category.CategoryName,
"Browse", new { Category = Category.CategoryName })
        </li>
    }
</ul>

并写在我的_Layout.cshtml:

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

                <ul class="nav navbar-nav">

                    <div class="dropdown">
                        <button class="dropbtn">@Html.Partial("CategoryLayout")</button>
                        <div class="dropdown-content">
                            <a href="#">Link 1</a>
                            <a href="#">Link 2</a>
                            <a href="#">Link 3</a>
                        </div>
                    </div>



                </ul>
                }

【问题讨论】:

  • 显示您的代码。
  • 如果类别不会更改,您可以将它们缓存在应用程序状态中,该状态位于服务器端。看到这个docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/…
  • @Reshma 我编辑并添加了一些代码,请检查
  • @BarryO'Kane 类别以后可能会改变,所以我想从 SQL 服务器收集数据
  • 你的代码没问题。我怀疑这些类别很少会改变,所以如果你想缓存它们,我再次建议将它们存储在应用程序状态中。

标签: html asp.net-mvc twitter-bootstrap layout partial-views


【解决方案1】:

首先

编辑

  1. 创建一个返回局部视图的 Action 方法

不要使用索引返回局部视图,因为异步调用需要局部视图,编写一个新的Action。

    public ActionResult GetCategories ()
    {
        var Categories = storeDB.Categories.ToList();
        return PartialView("CategoryLayout",Categories );
    }

上面的代码会用Categories模型渲染局部视图,并返回给Layout View。

  1. 在布局页面中而不是

    @Html.Partial("CategoryLayout") 
    

    使用

    @Html.RenderAction("GetCategories","Home")
    

完成!!!

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2012-08-30
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多