ASP.NET MVC 4 - Redirect after Modal Save using Child Action

前端 未结 1 707
迷失自我
迷失自我 2021-02-10 05:28

UPDATE - There are a lot of posts regarding the Child actions are not allowed to perform redirect actions on SO and ASP.NET forums. The

相关标签:
1条回答
  • 2021-02-10 06:01

    Try this for your modal:

    <div class="modal hide fade in" id="myModal" )>
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3>Modal header</h3>
          </div>
          @using (Html.BeginForm("Create", "Franchise", FormMethod.Post))
          {
             <div id="create_franchise_modal_body" class="modal-body">
               @Html.RenderPartial("Create")
             </div>
             <div class="modal-footer">
               <a href="#" class="btn" data-dismiss="modal">Close</a>
               <button type="submit" class="btn btn-primary">Save changes</button>
             </div>
          }
     </div>
    

    And then in your controller, fix your GET method so it returns a partial view result

    //
    // GET: /Franchise/Create
    
    public PartialViewResult Create(int FranchiseSetId)
    {
        ViewBag.PossibleFranchiseSets = franchisesetRepository.All;
        var franchise = new Franchise { FranchiseSetId = FranchiseSetId };
        return PartialView(franchise);
    } 
    

    I've made a couple of assumptions in this fix.

    1. Your "Create" view is supposed to be a partial view that has all of your form inputs
    2. When you call @Html.Action("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID }) you are wanting to render that partial view
    3. You're wanting the "Save Changes" link at the bottom of the modal to actually save your modal and not the "Create" button that is displayed

    If I've missed something let me know and I'll update my answer to try and provide a better solution.

    0 讨论(0)
提交回复
热议问题