0
所以我必須將數據發送到我的控制器一些JavaScript代碼:MVC3 JSON返回到編輯頁面
的Javascript:
<script type="text/javascript">
$(document).ready(function() {
$("#newGrade").click(function() {
var newGradeName = $("#newGradeName").val();
var newGradeValue = $("#newGradeValue").val();
var vSchoolID = $("#SchoolID").val();
if (newGradeName != null && newGradeValue != null) {
$.ajax({
url: '@Url.Action("NewGrade", "School")',
data: { gradeName: newGradeName, gradeValue: newGradeValue, schoolID: vSchoolID },
type: 'POST',
traditional: true,
success: function (data) {
if (data.status)
window.location = data.route;
},
error: function() {
return false;
}
});
}
});
});
</script>
控制器:
public ActionResult NewGrade(String gradeName, Int32 gradeValue, Guid schoolID)
{
School school = schoolRepository.GetByID(schoolID);
school.Grades.Add(
new Grade
{
GradeID = Guid.NewGuid(),
Name = gradeName,
NumericalValue = gradeValue
});
schoolRepository.Update(school);
schoolRepository.Save();
if (Request.IsAjaxRequest())
{
var json = new { status = true, route = Url.RouteUrl(new { action = "Edit", id = schoolID }) };
return Json(json, JsonRequestBehavior.AllowGet);
}
return View();
}
我現在的問題是我想返回到我的編輯頁面(可能刷新頁面,但不是數據,或只刷新整個頁面),但我的編輯頁面需要一個ID(學校ID)。按這裏所示,當按鈕進入編輯頁面:
<a href="@Url.Action("Edit", "School", new { id = Model.School.SchoolID })"><i class="icon-pencil"></i> Edit</a>
我的回答是:'{status = true,route =「/ Administrator/School/Edit/6cc2bbe1-4a4a-4c08-a1f1-001a25e9b988」} – 2012-08-03 15:12:25
@WesleyPattison:那麼問題是什麼?是window.location.href不工作? – Shyju 2012-08-03 15:15:47
不是,它返回到:http:// localhost:5591/Administrator/School更改的javascript:成功:函數(數據)如果(data.status) window.location.href = data.route; } 我查看了Chrome調試器,我也看到了對javascript的更改。 – 2012-08-03 15:16:27