1
我有一個ajax請求,對於局部視圖工作正常,並獲取我需要用戶編輯的信息,現在當用戶按下保存按鈕數據保存但它它保持重定向到不存在的CameraInfo/Index,是否可以保存並保留在同一頁面上?甚至返回到主頁,如果它不可能?下面的方法在HTTP發佈involded並獲取代碼涉及如何在保存部分視圖後保留在同一頁面
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);
}
如果是這種情況 – Matchbox2093
@goldeneye我更新了我的答案以反映您的情況 - 希望它很有用! – Alpha
謝謝@Alpha,只是一個小問題,說我想導航並提出一個新的當前視圖的新版本,我是否仍然需要將其更改爲通過javascript保存?或者我可以保留我擁有的? – Matchbox2093