【发布时间】:2015-06-11 23:09:33
【问题描述】:
在我的布局页面中,我有这个:
<div class="containerSidebar">
<div class="SidebarSection">
@RenderBody()
</div>
</div>
<div></div>
<div id="detailView"></div>
当我点击侧边栏中的链接时,它们会在 detailView 部分中打开。
当我单击详细视图中的提交按钮时,它会在渲染主体部分中打开,但我希望它继续在详细视图部分中打开。
例如,当我从该视图单击提交时:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>TestEmployee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Forename, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Forename, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Forename, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
它将转到控制器中的此部分:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult _NewEmpDetails([Bind(Include = "ID, Forename, Surname")] First NewEmpFirst)
{
if (ModelState.IsValid)
{
var sessionValues = new MySessionValues();
sessionValues.Employee = NewEmpFirst;
Session["MySessionValues"] = sessionValues;
}
return RedirectToAction("_NewEmpSecond");
}
我希望它直接到达 RedirectToAction 部分和 _Layout 页面中的 Jquery 或 Ajax 代码以捕获 _NewEmp... 并再次将其动态加载到 detaiView 部分中。
所以从 _Layout 页面调用的 jquery 文件中的类似内容
$("#detailView").submit(function () {
var form = $(this);
$.ajax({
type: "GET",
url: form.attr('RedirectToAction'),
success: function (data) {
//At this point I would like to redirect
$("#detailView").load($(this).attr("href"));
},
});
});
我真的很陌生,所以如果有人能提供帮助,那将非常感谢!
型号:
namespace OrwellFrontEnd.NewEmp
{
public class First
{
public int ID { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
}
public class Second
{
public int ID { get; set; }
public string Department { get; set; }
}
public class FirstSecond
{
public int ID { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string Department { get; set; }
}
public class Third
{
public int ID { get; set; }
public string Title { get; set; }
}
public class Fourth
{
public int ID { get; set; }
public bool FullTime { get; set; }
}
}
控制器名称公共类 TreeviewController : 控制器
要加载的第一页 _NewEmpDetails > _NewEmpSecond > _NewEmpThird > _NewEmpFourth
【问题讨论】:
-
首先
div不会有提交操作,而form会有。如果您对JsonResult有一点了解,那么您可以做到这一点。 -
表单提交正常吗?唯一的问题是我无法在局部区域中打开下一个局部视图。
-
提交是因为您的
input type = submit。要加载不同的局部视图,您需要大量修改代码。请提供更多详细信息,我可能会对您有所帮助。 -
它加载了正确的局部视图,唯一的问题是它在布局页面的 renderbody 部分中将其加载为完整视图,因为我想停止该操作并将其加载到 detailView 部分中像这样: $("#detailView").load($(this).attr("href"));哪些信息/代码最能帮助您?我很乐意提供。寻找允许提交的东西,更新会话变量,然后捕获要加载的部分视图并将其加载到详细视图部分
-
您没有取消标准提交。如图所示,您的代码将执行 ajax 调用,然后立即执行标准提交和重定向。
标签: jquery ajax asp.net-mvc asp.net-mvc-5