我正在尝试显示错误按摩作为模态boostrap ...消息正确发送但部分视图加载为常见页面...(无模态)
我需要你的帮助来解决这个问题.. :)
return RedirectToAction("ShowError", new { sErrorMessage = "Error Message" });
这是我的ActionResult'ShowError'
public ActionResult ShowError(String sErrorMessage)
{
ViewBag.sErrMssg = sErrorMessage;
return PartialView("ErrorMessageView");
}
我的PartialView'ErrorMessageView'
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Error</h4>
</div>
<div class="modal-body">
@ViewBag.sErrMssg
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
答案 0 :(得分:3)
您可以尝试以下解决方案:
ViewBag
而不是TempData["sErrMsg"]
使用TempData
- 使用TempData["sErrMsg"]
保留当前和后续HTTP请求的值。TempData["sErrMsg"]
设置为所需的错误消息。Html.RenderPartial("ErrorMessageView", TempData["sErrMsg"]);
是否有值(包含错误)以及是否使用public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Save(string name)
{
if(String.IsNullOrEmpty(name))
TempData["sErrMsg"] = "Product name cannot be empty";
return View("Index");
}
public PartialViewResult ShowError(String sErrorMessage)
{
return PartialView("ErrorMessageView");
}
}
将部分视图呈现为引导模式弹出窗口。<强>控制器:强>
@using (Html.BeginForm("Save", "Product", FormMethod.Post, new { id = "myform" }))
{
@Html.TextBox("name", null, new { style = "width:500px;" })
<input type="submit" value="Add" />
}
@if (TempData["sErrMsg"] != null)
{
Html.RenderPartial("ErrorMessageView", TempData["sErrMsg"]);
}
索引视图:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script type="text/javascript">
$(function () {
$('.modal').modal('show');
});
</script>
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Error</h4>
</div>
<div class="modal-body">
@Model
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
部分错误视图:
$(document).ready(function(){
var favorite = [];
$.each($("input[name='sport']:checked"), function() {
favorite.push($(this).val());
});
$("button1").click(function() {
$("#myModal").modal('show').on('shown.bs.modal', function() {
$("#checkid").html("I play these games " +"<br>" + favorite.join("<br>"));
});
});
$("button2").click(function() {
$("#myModal2").modal('show').on('shown.bs.modal', function() {
$("#checkid").html("I dont Play these games " +"<br>" + favorite.join("<br>"));
});
});
});