str*_*and 23 c# jquery asp.net-mvc-3 twitter-bootstrap
我目前正在使用Twitter的Bootstrap工具包进行新项目,我对在ASP.NET MVC3中使用模式对话框的最佳方法提出了疑问.
最好的做法是让Partial包含模态的标记然后使用javascript将其呈现到页面上还是有更好的方法?
zje*_*rry 56
这里是我的小教程,它演示了Twitter的Bootstrap(2.x)模式对话框,它与ASP.Net MVC 4中的表单和部分一起使用.
要下载类似的项目,但目标是MVC 5.1和Bootstrap 3.1.1,请访问此站点.
从空的MVC 4 Internet模板开始.
使用NuGet添加对Bootstrap的引用
在App_Start/BundleConfig.cs中添加以下行:
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
bundles.Add(new StyleBundle("~/Content/bootstrap").Include(
"~/Content/bootstrap.css",
"~/Content/bootstrap-responsive.css"));
Run Code Online (Sandbox Code Playgroud)
在Views/Shared/_Layout.cshtml中修改@ styles.Render行,使其看起来像:
@Styles.Render("~/Content/css", "~/Content/themes/base/css", "~/Content/bootstrap")
Run Code Online (Sandbox Code Playgroud)
和@ Scripts.Render行:
@Scripts.Render("~/bundles/jquery", "~/bundles/jqueryui", "~/bundles/bootstrap")
Run Code Online (Sandbox Code Playgroud)
到目前为止,我们已经准备好使用Bootstrap来使用MVC 4,所以让我们在/ Models文件夹中添加一个简单的模型类MyViewModel.cs:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
在HomeController中添加以下行:
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");
}
Run Code Online (Sandbox Code Playgroud)
在Views/Home文件夹中创建新的Partial View并将其命名为_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>
}
Run Code Online (Sandbox Code Playgroud)
在Home/Index.cshtml中,从模板中删除默认内容并将其替换为以下内容:
@{
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>
}
Run Code Online (Sandbox Code Playgroud)
如果运行应用程序,单击主页上的"创建"按钮后,将显示一个很好的Bootstrap模式.
尝试取消注释SaveChanges() //throw
HomeController.cs中的行,以证明您的控制器处理错误将在对话框中正确显示.
我希望我的示例澄清了在MVC应用程序中合并Bootstrap和创建模态的整个过程.