Ajax.ActionLink 和 Ajax.BeginForm 使用 MVC 局部视图
Ajax.ActionLink and Ajax.BeginForm with MVC Partial view
又来这么早伤心
我正在 mvc4 中实现基于选项卡的局部视图。我有 3 个选项卡,显示 3 个单独的局部视图,这些视图使用 Ajax.ActionLink 进行更改。每个部分视图都是需要发布到同一控制器内不同操作的表单。 ActionLinks 工作正常{部分视图得到显示和更改}。我已经使用 Ajax.BeginForm 提交个人表格,以防止完全回发。在表单提交上,操作被调用并实施,但 我的挑战 ,我需要显示当前表单但表单已清除,当前表单内容保留 - 即重定向到相同的 Partialview
下面是我的代码
项目的 ProjectIndex 操作 Controller.cs
[HttpPost]
public PartialViewResult Projectindex(Project collection, int ProjId)
{
//Save collection to DB.
return PartialView("_Project");
}
_Project.cshtml..局部视图
@model MyProject.Models.Project
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Ajax.BeginForm("Projectindex", new { ProjId = "3" }, new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "myonbegin",
OnSuccess = "myonsuccess",
OnComplete = "myoncomplete",
OnFailure = "myonfailure",
LoadingElementId = "loader" // div with .gif loader - that is shown when data are loading
}))
{ @Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Project</legend>
<div class="editor-label">
@Html.EditorFor(model => model.ProjName)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Project.cshtml 主视图
@Ajax.ActionLink(
"Project", //Link Text
"ProjectIndex", //Public Method inside our Controller
new AjaxOptions
{
HttpMethod = "GET", //Do a HTTP Post
InsertionMode = InsertionMode.Replace, //Replace content
UpdateTargetId = "TabBody", //Target element ID
}
)
<div id="TabBody">
@Html.Partial("_Project")
</div>
唯一的挑战是表单没有被清除,给我的印象是视图没有呈现。我尝试添加 OnSuccess 和 OnFailure 选项,但是 none 被解雇了。有人可以指点我吗在正确的方向。提前致谢
我只想将一个空模型发送回部分:
public PartialViewResult Projectindex(Project collection, int ProjId)
{
//Save collection to DB.
var model = new Project();
ModelState.Clear();
return PartialView("_Project", model);
}
添加了 ModelState.Clear()
,我认为这就是真正需要的。
Forces the html helpers use your changed model. Otherwise they use the values from the form submit
又来这么早伤心 我正在 mvc4 中实现基于选项卡的局部视图。我有 3 个选项卡,显示 3 个单独的局部视图,这些视图使用 Ajax.ActionLink 进行更改。每个部分视图都是需要发布到同一控制器内不同操作的表单。 ActionLinks 工作正常{部分视图得到显示和更改}。我已经使用 Ajax.BeginForm 提交个人表格,以防止完全回发。在表单提交上,操作被调用并实施,但 我的挑战 ,我需要显示当前表单但表单已清除,当前表单内容保留 - 即重定向到相同的 Partialview 下面是我的代码
项目的 ProjectIndex 操作 Controller.cs
[HttpPost]
public PartialViewResult Projectindex(Project collection, int ProjId)
{
//Save collection to DB.
return PartialView("_Project");
}
_Project.cshtml..局部视图
@model MyProject.Models.Project
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Ajax.BeginForm("Projectindex", new { ProjId = "3" }, new AjaxOptions
{
HttpMethod = "POST",
OnBegin = "myonbegin",
OnSuccess = "myonsuccess",
OnComplete = "myoncomplete",
OnFailure = "myonfailure",
LoadingElementId = "loader" // div with .gif loader - that is shown when data are loading
}))
{ @Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Project</legend>
<div class="editor-label">
@Html.EditorFor(model => model.ProjName)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Project.cshtml 主视图
@Ajax.ActionLink(
"Project", //Link Text
"ProjectIndex", //Public Method inside our Controller
new AjaxOptions
{
HttpMethod = "GET", //Do a HTTP Post
InsertionMode = InsertionMode.Replace, //Replace content
UpdateTargetId = "TabBody", //Target element ID
}
)
<div id="TabBody">
@Html.Partial("_Project")
</div>
唯一的挑战是表单没有被清除,给我的印象是视图没有呈现。我尝试添加 OnSuccess 和 OnFailure 选项,但是 none 被解雇了。有人可以指点我吗在正确的方向。提前致谢
我只想将一个空模型发送回部分:
public PartialViewResult Projectindex(Project collection, int ProjId)
{
//Save collection to DB.
var model = new Project();
ModelState.Clear();
return PartialView("_Project", model);
}
添加了 ModelState.Clear()
,我认为这就是真正需要的。
Forces the html helpers use your changed model. Otherwise they use the values from the form submit