我有一个我无法弄清楚的新手问题,我有一个从 asp.net 控制器的部分视图生成的表单,然后显示在 jquery 对话框中。
我在对话框上有 2 个按钮(保存/取消)
在保存按钮上,我想序列化表单中的输入以将其发送回 Asp.net mvc actionController,但是它似乎不起作用,actioncontroller 没有从 Jquery Dialog 获取模型对象,我正在使用表单的 Jquery 序列化函数。
这是脚本代码:
<button id="btnDialog">Account Logon</button>
<div id="Logonform"></div>
<script type="text/javascript">
$(document).ready(function () {
$.validator.unobtrusive.parse("#Logonform");
$("#Logonform").dialog({
autoOpen: false,
modal: true,
title: 'Login',
buttons: {
Save: function () {
alert($("#Logonform").serializeArray());
alert($("#Logonform").attr('UserName'));
$.ajax({
url: "@Url.Action("LogOn", "Account")",
type: "POST",
data: $("#Logonform").serialize(),
datatype: "JSON",
success: function (result) {
$("#Logonform").html(result).dialog('open');
}
});
},
Close: function () {
$(this).dialog('close');
}
}
});
$("#btnDialog").click(function () {
$.ajax({
url: "@Url.Action("LogOn", "Account")",
type: "GET",
success: function (result) {
$("#Logonform").html(result).dialog('open');
}
});
});
})
</script>
控制器:
public ActionResult LogOn()
{
if (Request.IsAjaxRequest())
{
return PartialView("_Logon");
}
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return Json( new { result = "ok", user = model.UserName });
}
else
{
return PartialView("_Logon");
}
}
风景:
@model JqueryDialogTest.Models.LogOnModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Models/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true, "Échec de la connexion. Corrigez les erreurs et réessayez.")
@using (Html.BeginForm()) {
<div>
<fieldset>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
</fieldset>
</div>
}
您的任何评论指出错误将不胜感激
干杯