What's the best way to call a modal dialog in ASP.NET MVC using Twitter Bootstrap?

后端 未结 4 1249
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 10:48

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

4条回答
  •  抹茶落季
    2020-12-04 10:53

    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
    
    
    
    @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @class = "modal-form" }))
    {
    @Html.ValidationSummary()
    
    
    
    
    
    }
    

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

    @{
        ViewBag.Title = "Home Page";
    }
    
    


    @Html.ActionLink("Create", "Create", null, null, new { id = "btnCreate", @class = "btn btn-small btn-info" }) @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

    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.

提交回复
热议问题