所以我不确定这里发生了什么。我有这个从动作加载部分视图的模式弹出窗口(这可能是问题??)。部分视图加载完美并且处理良好,但是一旦帖子结束并且应该返回一个 json,而不是模式只是关闭然后发布结果,它将我重定向到显示 json 结果的另一个页面(而不是部分视图)。我不完全确定我接近这个的方式是否正确。我只需要在操作成功处理后关闭对话框,然后如果它保存了事务或抛出错误,则返回一条消息。
任何建议表示赞赏。谢谢!!!
<script type="text/javascript">
$(function () {
$('#modal-link').click(function () {
var href = this.href;
$('#load-modal-dialog').dialog({
modal: true,
open: function (event, ui) {
$(this).load(href, function (result) {
$('#new-registration').submit(function () {
$.getJSON(href, function (data) {
if (data.success == true) {
$('#messages').html('woo!');
} else {
$('#messages').html('dafuq');
}
this.dialog('close');
});
});
});
}
});
return false;
});
});
</script>
我在弹出窗口中加载的部分视图
<% using (Html.BeginForm("New", "Registration", FormMethod.Post, new { id = "new-registration" })){ %>
<h2>Register Participant:</h2>
<div class="">
<%: Html.ValidationSummary(null, new { @class = "" })%>
<div class="">
<div class="">Email Address:</div>
<div class="">
<%: Html.TextBox("Email", null, new { @class = "" })%>
</div>
</div>
<button type="submit" value="Submit">Register</button>
</div>
<% } %>
那个行动
public ActionResult New(){
return PartialView(context.Contest.FirstOrDefault(e => e.Id == 1));
}
[HttpPost]
public ActionResult New(FormCollection formCollection)
{
string email = formCollection["Email"].ToString();
try
{
if (email == "")
throw new Exception("Please provide an email address.");
Registration registration = new Registration
{
ContestId = 1,
Email = email
};
context.Registration.Add(registration);
context.SaveChanges();
return Json(new { success = "true", message = "User succesfully registered to Contest." });
}
catch (Exception ex)
{
//throw ex;
return Json(new { success = "false", message = ex.Message });
}
}