[英]Redirect Error in ASP.NET MVC 5
我使用asp.net mvc 5创建了一个表单,当我单击“提交”按钮时,它成功提交了数据。 这意味着它已成功在控制器中调用Create方法。 我加了
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>
Ajax用于降低级联。
这是创建动作代码:
[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");
}
}
编辑:索引操作:
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);
}
这是因为您的Create方法无效并且什么也不返回。
您可以尝试执行以下操作来重定向:
Response.Redirect("/[ControllerName]/[ActionName]");
但是,最好坚持使用MVC模式,
public ActionResult Create(ExamViewModel vm)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.