알 수없는 작은 초보자 문제가 있습니다. asp.net 컨트롤러의 부분보기에서 생성 된 양식을 가지고 있습니다. jquery 대화 상자에 표시됩니다.Jquery 대화 상자에서 Json 형식의 데이터를 Asp.net MVC 3 컨트롤러로
내가, 내가 Asp.net MVC actionController로 다시 보낼 양식에서 입력을 직렬화 할 저장 버튼에
(저장/취소) 대화 상자에 2 개 버튼이, 그러나 그것은 '아무튼 작동하지 않는 것, actioncontroller Jquery 대화 상자에서 모델 개체를 가져 오지 않습니다, 양식에 대한 Jquery Serialize 함수를 사용하고 있습니다. 여기
는 스크립트 코드 :<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>
}
잘못 매우 감사하게 될 것입니다 무슨
건배
안녕하세요, 귀하의 제안 주셔서 감사합니다, 그러나, 나는 데이터를 스크립트에서 변경 : $ ("# Logonform") : serialize() 데이터 : $ ('양식).(), 완벽하게 작동합니다. 이제는 actionController가 objet를 올바르게 가져옵니다.하지만 이제는 또 다른 문제가 있습니다. json 양식을 actionController 메서드로 보내기 전에 대화 상자에 클라이언트 유효성 검사가 없습니다. – dtjmsy
AJAX 호출에 JQuery를 사용하기 때문에 유효성 검사에 JQuery를 사용합니다. 유효성 검사에 대한 몇 가지 JQery 문서가 있습니다 (http://docs.jquery.com/Plugins/Validation). –