Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:47:09+00:00 2026-06-12T17:47:09+00:00

I’m currently using Twitter’s Bootstrap toolkit on a new project and I had a

  • 0

I’m currently using Twitter’s Bootstrap toolkit on a new project and I had a question on the best way to use the modal dialog in ASP.NET MVC3.

Is the best practice to have a Partial that contains the modal’s markup and then use javascript to render that onto the page or is there a better approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:47:11+00:00Added an answer on June 12, 2026 at 5:47 pm

    Here goes my little tutorial which demonstrates Twitter’s Bootstrap (2.x) modal dialog that works with forms and partials in ASP.Net MVC 4.

    To download similar project but targeting MVC 5.1 and Bootstrap 3.1.1 please visit this site.

    Start with an empty MVC 4 Internet template.

    Add reference to Bootstrap using NuGet

    In the App_Start/BundleConfig.cs add the following lines:

    bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
    bundles.Add(new StyleBundle("~/Content/bootstrap").Include(
                        "~/Content/bootstrap.css",
                        "~/Content/bootstrap-responsive.css"));
    

    In the Views/Shared/_Layout.cshtml
    modify the @styles.Render line so it will look like:

    @Styles.Render("~/Content/css", "~/Content/themes/base/css",  "~/Content/bootstrap")
    

    and the @Scripts.Render line:

    @Scripts.Render("~/bundles/jquery", "~/bundles/jqueryui",  "~/bundles/bootstrap")
    

    So far we have Bootstrap prepared to work with MVC 4 so let’s add a simple model class MyViewModel.cs to the /Models folder:

    using System.ComponentModel.DataAnnotations;
    
    namespace MvcApplication1.Models
    {
        public class MyViewModel
        {
            public string Foo { get; set; }
    
            [Required(ErrorMessage = "The bar is absolutely required")]
            public string Bar { get; set; }
        }
    }
    

    In the HomeController Add the following lines:

    using MvcApplication1.Models;
    //...
    
        public ActionResult Create()
        {
            return PartialView("_Create");
        }
    
        [HttpPost]
        public ActionResult Create(MyViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    SaveChanges(model);
                    return Json(new { success = true });
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.Message);
                }
    
            }
            //Something bad happened
            return PartialView("_Create", model);
        }
    
    
        static void SaveChanges(MyViewModel model)
        {
            // Uncommment next line to demonstrate errors in modal
            //throw new Exception("Error test");
        }
    

    Create new Partial View in the Views/Home folder and name it _Create.cshtml:

    @using MvcApplication1.Models
    @model MyViewModel
    
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Create Foo Bar</h3>
    </div>
    
    @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @class = "modal-form" }))
    {
    @Html.ValidationSummary()
    
    <div  class="modal-body">
        <div>
            @Html.LabelFor(x => x.Foo)
            @Html.EditorFor(x => x.Foo)
            @Html.ValidationMessageFor(x => x.Foo)
        </div>
        <div>
            @Html.LabelFor(x => x.Bar)
            @Html.EditorFor(x => x.Bar)
            @Html.ValidationMessageFor(x => x.Bar)
        </div>
    </div>
    
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Undo</button>
        <button class="btn btn-primary" type="submit">Save</button>
    </div>
    
    }
    

    In the Home/Index.cshtml remove the default content from the template and replace it with following:

    @{
        ViewBag.Title = "Home Page";
    }
    
    <br />
    <br />
    <br />
    
    @Html.ActionLink("Create", "Create", null, null, new { id = "btnCreate", @class = "btn btn-small btn-info" })
    
    <div id='dialogDiv' class='modal hide fade in'>
        <div id='dialogContent'></div>
    </div>
    
    @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    
    <script type="text/javascript">
        $(function () {
    
            //Optional: turn the chache off
            $.ajaxSetup({ cache: false });
    
            $('#btnCreate').click(function () {
                $('#dialogContent').load(this.href, function () {
                    $('#dialogDiv').modal({
                        backdrop: 'static',
                        keyboard: true
                    }, 'show');
                    bindForm(this);
                });
                return false;
            });
        });
    
        function bindForm(dialog) {
            $('form', dialog).submit(function () {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (result) {
                        if (result.success) {
                            $('#dialogDiv').modal('hide');
                            // Refresh:
                            // location.reload();
                        } else {
                            $('#dialogContent').html(result);
                            bindForm();
                        }
                    }
                });
                return false;
            });
        }
    
    </script>
    }
    

    If you run your application, a nice Bootstrap modal will appear after clicking the Create button on the Home page.

    Try to uncomment the SaveChanges() //throw line in HomeController.cs to prove that your controller handled errors will appear correctly in the dialog.

    I hope that my sample clarifies a bit whole process of incorporating Bootstrap and creating modals in the MVC application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.