简体   繁体   中英

Redirect Error in ASP.NET MVC 5

I have created a form using asp.net mvc 5 and when I click the submit button it sucessfully submiting the data. it means it successfully call Create method in my controller. I added

    RedirectToAction("Index", "ExaminationManager");

code to last line of the code in Create method. But this not redirect to "Index". It always redirect to "Create".

This is my view code :

@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 is used to drop down cascade.

This is Create action code :

    [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");
        }


    }

EDIT : Index action:

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);

    }

Its because your Create method is void and returns nothing.

You can try something like this to redirect:

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

However, it would be preferable to stick to the MVC pattern,

public ActionResult Create(ExamViewModel vm)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM