【问题标题】:Redirect Error in ASP.NET MVC 5ASP.NET MVC 5 中的重定向错误
【发布时间】:2014-06-20 16:31:44
【问题描述】:

我使用 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">&times;</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);

    }

【问题讨论】:

  • 能否请您显示索引操作的代码?
  • 我已使用索引操作编辑了我的原始帖子。谢谢。
  • 更新.Net Fiddle中的代码

标签: c# jquery ajax asp.net-mvc redirect


【解决方案1】:

因为你的 Create 方法是 void 并且什么也不返回。

你可以尝试这样的重定向:

Response.Redirect("/[ControllerName]/[ActionName]");

但是,最好还是坚持 MVC 模式,

public ActionResult Create(ExamViewModel vm)

【讨论】:

  • 非常感谢它与 Response.Redirect("Index");
猜你喜欢
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 2018-05-02
相关资源
最近更新 更多