3

I am creating a simple login page in ASP.net MVC with bootstrap

BundleConfig.cs

public class BundleConfig
    {

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.IgnoreList.Clear();

            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-1.9.1.js"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrapjs").Include(
                        "~/Scripts/bootstrap.min.js"));

            bundles.Add(new StyleBundle("~/Content/bootstrapcss").Include(
                        "~/Content/bootstrap.min.css",
                        "~/Content/bootstrap.css"));
        }

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/bootstrapcss")
</head>
<body>
    @RenderBody()
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrapjs")
</body>
</html>

View: Index.cshtml

@model ME.Models.Admin.AdminModel

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

<center>
<div class="panel panel-primary" style="display:inline-block">
    Hello Admin! Please Login
    <br />

    <table>
        <tr>
            <td>@Html.LabelFor(m=>m.username)</td>
            <td>@Html.TextBoxFor(m=>m.username, new { @class = "form-control" })<br /></td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m=>m.password)</td>
            <td>@Html.TextBoxFor(m=>m.password, new { @class = "form-control" })<br /></td>

        </tr>
        <tr>
            <td></td>
            <td><input type="submit" class="btn btn-primary" value="Log In" /></td>
        </tr>
    </table>

</div>
    </center>

On running the project, Controls are displayed but bootstrap classes are not applied to controls.

Under browser console I get 404 error for bundles:

/bundles/jquery
/bundles/bootstrapjs
/Content/bootstrapcss

I have used bundling and _Layout.cshtml in Shared directory under view folder.

Can someone help with this issue?

Note: I haven't pasted code of Model and controller since I believe it's not needed here.

Thanks.

9
  • Does the browser load the bundles or are there any errors? Why do you as both the minificated and the normal bootstrap css? Commented Jul 14, 2017 at 15:02
  • Try add BundleTable.EnableOptimizations = true; before creating a new bundle. Commented Jul 14, 2017 at 15:16
  • @BobSwager Do I have to add this in BundleConfig class?? Commented Jul 14, 2017 at 15:22
  • Yes. But i think that your solution should be something like this : stackoverflow.com/questions/13458642/… Commented Jul 14, 2017 at 15:23
  • @All. I am getting 404 error for /bundles/jquery, /bundles/bootstrapjs /Content/bootstrapcss. Do I have to create bundle directory?? Commented Jul 14, 2017 at 15:29

1 Answer 1

3
protected virtual void Application_Start()
    {
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

This was missing. Got it worked.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.