0

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

    }
3
  • Could you pl show code of Index action? Commented May 4, 2014 at 9:06
  • I have edited my original post with Index action. Thank you. Commented May 4, 2014 at 9:08
  • update your code in .Net Fiddle Commented May 4, 2014 at 9:26

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much it worked with Response.Redirect("Index");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.