我已經創建了一個使用asp.net mvc 5的表單,當我點擊提交按鈕它成功提交數據。這意味着它成功地在我的控制器中調用Create方法。我添加了在ASP.NET MVC中重定向錯誤5
RedirectToAction("Index", "ExaminationManager");
代碼在Create方法中的最後一行代碼。但是這不會重定向到「索引」。它總是重定向到「創建」。
這是我的看法代碼:
@model TutionWeb1.Models.ViewModels.ExaminationManagerVM.ExamViewModel
@using (Html.BeginForm("Create", "ExaminationManager", FormMethod.Post, new { id = "createForm" }))
{
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.SubjectID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SubjectCategoryID, ViewBag.SubjectCategoriyID as SelectList, "Select a Subject Category", new { id = "SubjectCategory", @class = "form-control" })
@Html.ValidationMessageFor(model => model.SubjectCategoryID)
<br/>
@Html.DropDownListFor(model => model.SubjectID, Enumerable.Empty<SelectListItem>(), "Select a Subject", new { id = "SubjectID", @class = "form-control" })
@Html.ValidationMessageFor(model => model.SubjectID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ExamName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.ExamName, new { @class = "form-control", placeholder = "Exam Name" })
@Html.ValidationMessageFor(model => model.ExamName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ExamNote, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.ExamNote, new { @class = "form-control", placeholder = "Description" })
@Html.ValidationMessageFor(model => model.ExamNote)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EnrollKey, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.EnrollKey, new { @class = "form-control", placeholder = "Enrollment Key" })
@Html.ValidationMessageFor(model => model.EnrollKey)
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Submit" />
</div>
</div>
</div>
</div>
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script type="text/jscript">
$(function() {
$('#SubjectCategory').change(function() {
$.getJSON('/ExaminationManager/SubjectsList/' + $('#SubjectCategory').val(), function (data) {
var items = '<option value="">Select a District</option>';
$.each(data, function (i, subject) {
items += "<option value='" + subject.Value + "'>" + subject.Text + "</option>";
});
$('#SubjectID').html(items);
});
});
});
</script>
阿賈克斯用於下拉級聯。
這是創建動作代碼:
[HttpPost]
public void Create(ExamViewModel vm)
{
if (ModelState.IsValid)
{
Mapper.CreateMap<ExamViewModel, Examination>();
var exam = new Examination();
Mapper.Map<ExamViewModel, Examination>(vm, exam);
exam.TutorID = KEY_TUTOR_ID;
service_exam.CreateExam(exam);
RedirectToAction("Index", "ExaminationManager");
}
}
編輯: Index操作:
public ActionResult Index(int? page)
{
var examinationdata = new ExaminationManagerViewModel();
ViewBag.SubjectCategoriyID = new SelectList(service_subject_category.GetSubjectCategories(KEY_LANG), "SubjectCategoryID", "SubjectCategoryName");
IEnumerable<SubjectsResult> subjects_mod = service_subject.GetSubjectsByTutor(KEY_TUTOR_ID,KEY_LANG);
Mapper.CreateMap<SubjectsResult, SubjectListViewModel>();
var subjects = Mapper.Map<IEnumerable<SubjectsResult>, List<SubjectListViewModel>>(subjects_mod);
examinationdata.Subjects = subjects;
IEnumerable<Examination> exams = service_exam.GetExams(KEY_TUTOR_ID).ToList();
Mapper.CreateMap<Examination, ExamRowViewModel>();
var attachments = Mapper.Map<IEnumerable<Examination>, List<ExamRowViewModel>>(exams);
examinationdata.Exam = new ExamViewModel();
examinationdata.Exams = attachments.ToList().ToPagedList(page ?? 1,3);
return View(examinationdata);
}
你能PL顯示指數的代碼行動? –
我編輯了我的原始帖子與索引行動。謝謝。 –
在[.Net小提琴](http://dotnetfiddle.net/) – super