保存局部视图后如何留在同一页面上
How to stay on same page after saving partial view
我有一个 ajax 部分视图请求工作正常,并获取了我需要用户编辑的信息,现在当用户按下保存按钮时,数据会保存,但它会一直重定向到 CameraInfo/Index 哪个不存在,是否可以保存并停留在同一页面上?如果不可能,甚至 return 到主页?下面是HTTPPost中涉及的方法和涉及的获取代码
AJAX
$(document).ready(function () {
$('#qr-number').on('change', function () {
$.ajax({
type: "Get",
url: '/CameraInfo/Edit',
data: { id: $('#qr-number').val() },
success: function (response) {
$('#Sample').html(response);
},
error: function (response) {
if (response.responseText != "") {
alert(response.responseText);
alert("Some thing wrong..");
}
}
});
});
});
_CameraInfo.cshtml(局部视图)
@model JobTracker.Models.Job
<h2>Edit and Confirm</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Job</legend>
@Html.HiddenFor(model => model.JobID)
@Html.HiddenFor(model => model.OrderID)
<div class="editor-label">
@Html.LabelFor(model => model.LocationID, "Location")
</div>
<div class="editor-field">
@Html.DropDownList("LocationID", String.Empty)
@Html.ValidationMessageFor(model => model.LocationID)
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.HighPriority)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.HighPriority, new SelectList(
new[]
{
new { Value = "Yes", Text = "Yes" },
new { Value = "No", Text = "No" },
},
"Value",
"Text",
Model
))
@Html.ValidationMessageFor(model => model.HighPriority)
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Comments)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Comments)
@Html.ValidationMessageFor(model => model.Comments)
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Status, new SelectList(
new[]
{
new { Value = "In Progress", Text = "In Progress" },
new { Value = "Completed", Text = "Completed" },
new { Value = "Not Started", Text = "Not Started" },
new { Value = "Stopped", Text = "Stopped" },
},
"Value",
"Text",
Model
))
@Html.ValidationMessageFor(model => model.Status)
</div><br />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
CameraInfo.cs
[HttpGet]
public ActionResult Edit(int id = 0)
{
Job job = db.Jobs.Find(id);
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);
return PartialView("_CameraInfo", job);
}
[HttpPost]
public ActionResult Edit(Job job)
{
if (ModelState.IsValid)
{
var LastUpdated = System.DateTime.Now;
job.LastUpdated = LastUpdated;
db.Entry(job).State = EntityState.Modified;
db.SaveChanges();
}
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);
return View(job);
}
如果是 AJAX 呼叫,则用户不应进行导航。有问题的代码很可能在视图中,可能是一种正在 POST 到 URL 而不是执行 AJAX 调用的表单?
编辑:现在您已经显示了查看代码,我的怀疑得到了证实。您不应使用常规表单,因为这会使用户在发布值时进行导航。另一种方法是使用 AjaxForms 而不是常规表单。另一个是阻止表单提交,而是通过 JavaScript.
自己提交值
我实际上对最后一个建议:使用 JavaScript 提交,但保留表格。因此,如果您的目标受众中有人可能已禁用 JavaScript,您可以通过使用常规 POST 和用户导航来围绕它进行设计。如果他们这样做,您可以通过在表单的提交事件中返回 false 来避免导航。
我有一个 ajax 部分视图请求工作正常,并获取了我需要用户编辑的信息,现在当用户按下保存按钮时,数据会保存,但它会一直重定向到 CameraInfo/Index 哪个不存在,是否可以保存并停留在同一页面上?如果不可能,甚至 return 到主页?下面是HTTPPost中涉及的方法和涉及的获取代码
AJAX
$(document).ready(function () {
$('#qr-number').on('change', function () {
$.ajax({
type: "Get",
url: '/CameraInfo/Edit',
data: { id: $('#qr-number').val() },
success: function (response) {
$('#Sample').html(response);
},
error: function (response) {
if (response.responseText != "") {
alert(response.responseText);
alert("Some thing wrong..");
}
}
});
});
});
_CameraInfo.cshtml(局部视图)
@model JobTracker.Models.Job
<h2>Edit and Confirm</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Job</legend>
@Html.HiddenFor(model => model.JobID)
@Html.HiddenFor(model => model.OrderID)
<div class="editor-label">
@Html.LabelFor(model => model.LocationID, "Location")
</div>
<div class="editor-field">
@Html.DropDownList("LocationID", String.Empty)
@Html.ValidationMessageFor(model => model.LocationID)
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.HighPriority)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.HighPriority, new SelectList(
new[]
{
new { Value = "Yes", Text = "Yes" },
new { Value = "No", Text = "No" },
},
"Value",
"Text",
Model
))
@Html.ValidationMessageFor(model => model.HighPriority)
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Comments)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Comments)
@Html.ValidationMessageFor(model => model.Comments)
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Status, new SelectList(
new[]
{
new { Value = "In Progress", Text = "In Progress" },
new { Value = "Completed", Text = "Completed" },
new { Value = "Not Started", Text = "Not Started" },
new { Value = "Stopped", Text = "Stopped" },
},
"Value",
"Text",
Model
))
@Html.ValidationMessageFor(model => model.Status)
</div><br />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
CameraInfo.cs
[HttpGet]
public ActionResult Edit(int id = 0)
{
Job job = db.Jobs.Find(id);
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);
return PartialView("_CameraInfo", job);
}
[HttpPost]
public ActionResult Edit(Job job)
{
if (ModelState.IsValid)
{
var LastUpdated = System.DateTime.Now;
job.LastUpdated = LastUpdated;
db.Entry(job).State = EntityState.Modified;
db.SaveChanges();
}
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);
return View(job);
}
如果是 AJAX 呼叫,则用户不应进行导航。有问题的代码很可能在视图中,可能是一种正在 POST 到 URL 而不是执行 AJAX 调用的表单?
编辑:现在您已经显示了查看代码,我的怀疑得到了证实。您不应使用常规表单,因为这会使用户在发布值时进行导航。另一种方法是使用 AjaxForms 而不是常规表单。另一个是阻止表单提交,而是通过 JavaScript.
自己提交值我实际上对最后一个建议:使用 JavaScript 提交,但保留表格。因此,如果您的目标受众中有人可能已禁用 JavaScript,您可以通过使用常规 POST 和用户导航来围绕它进行设计。如果他们这样做,您可以通过在表单的提交事件中返回 false 来避免导航。