Asp.net mvc 3 mvc 3-httppost上的ajax.begin表单和部分视图
我对partialView有问题,无法在使用Ajax.PostBack发布后加载partialView。当我单击“提交”按钮时,partialView渲染为视图而不是部分视图。这是我的控制器:Asp.net mvc 3 mvc 3-httppost上的ajax.begin表单和部分视图,asp.net-mvc-3,partial-views,Asp.net Mvc 3,Partial Views,我对partialView有问题,无法在使用Ajax.PostBack发布后加载partialView。当我单击“提交”按钮时,partialView渲染为视图而不是部分视图。这是我的控制器: [HttpPost] public PartialViewResult UpdatePersonalData(UserLine user) { var usr = um.GetUserByLoginMapper(User.Id
[HttpPost]
public PartialViewResult UpdatePersonalData(UserLine user)
{
var usr = um.GetUserByLoginMapper(User.Identity.Name);
ViewBag.Regions = rm.GetAllRegionsMapper().ToList();
if (ModelState.IsValid)
{
return PartialView("getLabelsPersonalData", usr);
}
return PartialView("getPersonalData", user);
}
public PartialViewResult getPersonalData()
{
ViewBag.Regions = rm.GetAllRegionsMapper().ToList();
var user = um.GetUserByLoginMapper(User.Identity.Name);
return PartialView("getPersonalData", user);
}
public PartialViewResult getLabelsPersonalData()
{
var user = um.GetUserByLoginMapper(User.Identity.Name);
return PartialView("getLabelsPersonalData", user);
}
getPersonalData.cshtml-partialView
@model MasterProject.JobForYouFinal.BO.UserLine
@using (Ajax.BeginForm("UpdatePersonalData", new AjaxOptions
{
UpdateTargetId = "personalDataContent",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST"
}))
{
<table style="text-align:center; margin:0 auto;">
<tr>
<td>@Html.LabelFor(a => a.PersonalData.Name)</td>
<td>@Html.EditorFor(a => a.PersonalData.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.LastName)</td>
<td>@Html.EditorFor(a => a.PersonalData.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.Address.PostCode)</td>
<td>@Html.EditorFor(a => a.Address.PostCode)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.KeyWord)</td>
<td>@Html.EditorFor(a => a.PersonalData.KeyWord)</td>
</tr>
<tr>
<td><input id="save" type="submit" value="Zapisz" /></td>
<td>@Ajax.ActionLink("Anuluj", "getLabelsPersonalData", new AjaxOptions {
UpdateTargetId = "personalDataContent",
InsertionMode = InsertionMode.Replace
})</td>
</tr>
<tr>
<td>@Html.ValidationSummary()</td>
</tr>
</table>
}
@model MasterProject.JobForYouFinal.BO.UserLine
<table class="myAccountTable">
<tr>
<td>@Html.LabelFor(a => a.RegistrationDate)</td>
<td>@Html.DisplayFor(a => a.RegistrationDate)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.Name)</td>
<td>@Html.DisplayFor(a => a.PersonalData.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.LastName)</td>
<td>@Html.DisplayFor(a => a.PersonalData.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.KeyWord)</td>
<td>@Html.DisplayFor(a => a.PersonalData.KeyWord)</td>
</tr>
<tr>
<td colspan="2"><input id="personalDataButton" type="submit" value="Edytuj" /></td>
</tr>
</table>
index.cshtml = glowny widok
<div class="empAccountSectionContainer">
<div id="personalDataHeader" class="empAccountSectionHeader">Dane personalne</div>
@using (Ajax.BeginForm("getPersonalData", new AjaxOptions
{
UpdateTargetId = "personalDataContent",
InsertionMode = InsertionMode.Replace
}))
{
<div id="personalDataContent" class="empAccountSectionBody">
@{Html.RenderPartial("getLabelsPersonalData", Model.User);}
</div>
}
</div>
@model MasterProject.JobForYouFinal.BO.UserLine
@使用(Ajax.BeginForm(“UpdatePersonalData”),新的AjaxOptions
{
UpdateTargetId=“personalDataContent”,
InsertionMode=InsertionMode.Replace,
HttpMethod=“POST”
}))
{
@Html.labelfar(a=>a.PersonalData.Name)
@EditorFor(a=>a.PersonalData.Name)
@LabelFor(a=>a.PersonalData.LastName)
@EditorFor(a=>a.PersonalData.LastName)
@LabelFor(a=>a.Address.PostCode)
@EditorFor(a=>a.Address.PostCode)
@LabelFor(a=>a.PersonalData.KeyWord)
@EditorFor(a=>a.PersonalData.KeyWord)
@ActionLink(“Anuluj”,“getLabelsPersonalData”,新的AjaxOptions{
UpdateTargetId=“personalDataContent”,
InsertionMode=InsertionMode.Replace
})
@Html.ValidationSummary()
}
getLabelPersonalData.cshtml-partialView
@model MasterProject.JobForYouFinal.BO.UserLine
@using (Ajax.BeginForm("UpdatePersonalData", new AjaxOptions
{
UpdateTargetId = "personalDataContent",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST"
}))
{
<table style="text-align:center; margin:0 auto;">
<tr>
<td>@Html.LabelFor(a => a.PersonalData.Name)</td>
<td>@Html.EditorFor(a => a.PersonalData.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.LastName)</td>
<td>@Html.EditorFor(a => a.PersonalData.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.Address.PostCode)</td>
<td>@Html.EditorFor(a => a.Address.PostCode)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.KeyWord)</td>
<td>@Html.EditorFor(a => a.PersonalData.KeyWord)</td>
</tr>
<tr>
<td><input id="save" type="submit" value="Zapisz" /></td>
<td>@Ajax.ActionLink("Anuluj", "getLabelsPersonalData", new AjaxOptions {
UpdateTargetId = "personalDataContent",
InsertionMode = InsertionMode.Replace
})</td>
</tr>
<tr>
<td>@Html.ValidationSummary()</td>
</tr>
</table>
}
@model MasterProject.JobForYouFinal.BO.UserLine
<table class="myAccountTable">
<tr>
<td>@Html.LabelFor(a => a.RegistrationDate)</td>
<td>@Html.DisplayFor(a => a.RegistrationDate)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.Name)</td>
<td>@Html.DisplayFor(a => a.PersonalData.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.LastName)</td>
<td>@Html.DisplayFor(a => a.PersonalData.LastName)</td>
</tr>
<tr>
<td>@Html.LabelFor(a => a.PersonalData.KeyWord)</td>
<td>@Html.DisplayFor(a => a.PersonalData.KeyWord)</td>
</tr>
<tr>
<td colspan="2"><input id="personalDataButton" type="submit" value="Edytuj" /></td>
</tr>
</table>
index.cshtml = glowny widok
<div class="empAccountSectionContainer">
<div id="personalDataHeader" class="empAccountSectionHeader">Dane personalne</div>
@using (Ajax.BeginForm("getPersonalData", new AjaxOptions
{
UpdateTargetId = "personalDataContent",
InsertionMode = InsertionMode.Replace
}))
{
<div id="personalDataContent" class="empAccountSectionBody">
@{Html.RenderPartial("getLabelsPersonalData", Model.User);}
</div>
}
</div>
@model MasterProject.JobForYouFinal.BO.UserLine
@LabelFor(a=>a.RegistrationDate)
@DisplayFor(a=>a.RegistrationDate)
@LabelFor(a=>a.PersonalData.Name)
@DisplayFor(a=>a.PersonalData.Name)
@LabelFor(a=>a.PersonalData.LastName)
@DisplayFor(a=>a.PersonalData.LastName)
@LabelFor(a=>a.PersonalData.KeyWord)
@DisplayFor(a=>a.PersonalData.KeyWord)
index.cshtml=glowny-widok
丹麦人
@使用(Ajax.BeginForm(“getPersonalData”),新的AjaxOptions
{
UpdateTargetId=“personalDataContent”,
InsertionMode=InsertionMode.Replace
}))
{
@{Html.RenderPartial(“getLabelsPersonalData”,Model.User);}
}
正在尝试使用某些onSuccess函数修复此问题:
<script type="text/javascript">
function onSuccess() {
$('#personalDataContent').load('EmployeeAccount/getLabelsPersonalData');
}
</script>
函数onSuccess(){
$(“#personalDataContent”).load('EmployeeAccount/getLabelsPersonalData');
}
这是我的版式导入
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/start/jquery-ui.css" rel='stylesheet' type='text/css'>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.4.min.js"></script>
<script str="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-1.7-development-only.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.11/jquery-ui.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmplPlus.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>
<script str="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.min.js"></script>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/Styles.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery-ui-1.8.22.custom.css")" rel="Stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.ui.dialog.css")" rel="Stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.ui.theme.css")" rel="Stylesheet" type="text/css" />
</head>
I had to pust this:
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
@视图包。标题
我不得不说:
在我看来,因为验证不起作用。现在验证工作正常,但当模型有效时,不能将局部视图作为局部加载
但仍然没有结果。请帮忙 我知道这是个老问题,但我也有同样的问题。在我看来,我用下面的代码解决了这个问题:
@{
Layout = Request.IsAjaxRequest()?null:"~/Views/Shared/Layout.cshtml";
}
我不知道这个解决方案是否是最好的,但它确实有效;-) 您是否在视图/布局中引用了
“~/Scripts/jquery.unobtrusive ajax.js”
?我有所有必要的脚本参考和partialView仍然不工作