将引导程序与MVC集成 [英] Integrating bootstrap with MVC

查看:76
本文介绍了将引导程序与MVC集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!

我正在学习bootstrap。我使用layoutit.com创建了一个bootstrap模板。我想了解在index.cshtml和_layout.cshtml中需要做哪些更改才能在我的VS2015 MVC项目中实现创建的引导程序模板。



期待回答。谢谢。



我尝试了什么:



我已经包括内容文件夹中的css文件和解决方案中脚本文件夹中的js文件。我尝试将layoutit.com提供的html代码添加到我的index.schtml页面上创建模板,但是开发的pafe是分散的。我也尝试在index.cshtml页面中添加css链接,如下所示:

< link href =〜/ css / bootstrap.css =stylesheet/> 



我在bundleconfig.cs中注册了样式和脚本包,如下所示:

 public class BundleConfig 
{
public static void RegisterBundles(BundleCollection bundle)
{
StyleBundle(bundles);
ScriptBundle(捆绑);
}

public static void StyleBundle(BundleCollection包)
{
bundles.Add(new StyleBundle(〜/ css)
.Include( 〜/内容/ bootstrap.css));
}

public static void ScriptBundle(BundleCollection bundle)
{
bundles.Add(new ScriptBundle(〜/ js)
.Include( 〜/ Scripts / jquery-1.10.2.js)
.Include(〜/ Scripts / bootstrap.js));
}
}

解决方案

这是一个在单个页面上使用bootstrap的示例,脚本和样式装饰应相同。



Bootstrap菜单 [ ^ ]



我看到你的代码在bundleconfig.cs中注册了样式和脚本包但是布局页面是否渲染了它?另外,请验证您的样式和脚本位置,确保文件位于正确的位置。

 @ Styles.Render(〜/ css)
@ Scripts.Render(〜/ js)



我认为这个引用是错误的,应该是〜/ Content / bootstrap.css?

 <   link     href   = 〜/ css / bootstrap.css    rel   = 样式表    /  >  


问题已解决。我错过了以下部分:

转到共享文件夹。在_Layout.cshtml中写下以下代码 -

<!DOCTYPE html> 
< html lang =en>
< head>
< meta charset =utf-8/>
< title> @ ViewBag.Title - 我的ASP.NET MVC应用程序< / title>
< link href =〜/ Content / bootstrap.min.css =stylesheet/>
< link href =〜/ Content / bootstrap-theme.min.css =stylesheet/>

< / head>
< body>

< div class =container>
@RenderBody()
< / div>

< script src =〜/ Scripts / jquery-1.10.2.min.js>< / script>
< script src =〜/ Scripts / modernizr-2.6.2.js>< / script>
< script src =〜/ Scripts / respond.min.js>< / script>
< script src =〜/ Scripts / bootstrap.min.js>< / script>
< / body>
< / html>


Greetings!
I am learning bootstrap. I have created a bootstrap template using layoutit.com . I want to understand what changes do I need to do in index.cshtml and _layout.cshtml to implement the created bootstrap template in my VS2015 MVC project.

Looking forward for an answer. Thank you.

What I have tried:

I have included css files in content folder and js files in script folder in my solution. I tried adding the html code provided by layoutit.com on creation of template to my index.schtml page but the pafe developed is scattered. I also tried adding css link in index.cshtml page like this:

<link href="~/css/bootstrap.css" rel="stylesheet" />


I have registered style and script bundle in bundleconfig.cs like this:

public class BundleConfig
   {
       public static void RegisterBundles(BundleCollection bundles)
       {
           StyleBundle(bundles);
           ScriptBundle(bundles);
       }

       public static void StyleBundle(BundleCollection bundles)
       {
           bundles.Add(new StyleBundle("~/css")
                    .Include("~/Content/bootstrap.css"));
       }

       public static void ScriptBundle(BundleCollection bundles)
       {
           bundles.Add(new ScriptBundle("~/js")
                    .Include("~/Scripts/jquery-1.10.2.js")
                    .Include("~/Scripts/bootstrap.js"));
       }
   }

解决方案

Here is an example using bootstrap on a single page, the scripts and styles decoration shall be the same.

Bootstrap Menu[^]

I see your code has registered style and script bundle in bundleconfig.cs but did the layout page render it? Also, verify your styles and scripts location, make sure the files are in the correct location.

@Styles.Render("~/css")
@Scripts.Render("~/js")


I think this reference is wrong, should it be ~/Content/bootstrap.css?

<link href="~/css/bootstrap.css" rel="stylesheet" />


The issue is resolved. I missed out the following part:
Go to Shared folder.Write the following code in the _Layout.cshtml -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title - My ASP.NET MVC Application</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-theme.min.css" rel="stylesheet" />

</head>
<body>

    <div class="container">
        @RenderBody()
    </div>

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/modernizr-2.6.2.js"></script>
    <script src="~/Scripts/respond.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
</body>
</html> 


这篇关于将引导程序与MVC集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆