ASP.NET MVC 4 - Redirection after Modal Save with Child Action


UPDATE . There are many reports that Child Actions are not allowed to perform redirect actions on the SO and ASP.NET forums. The solution below seems to be the easiest workaround I've seen for this problem yet. If you have this problem, I hope you find it useful. If there is a better one, I am certainly open to suggestions.


I spent all day this whole brain in my brain ...

I have a FranchiseSet model and a Franchise model - the relationship is that FranchiseSet has a lot of franchises.

The FranchiseSet index view has a Franchise Sets table where each row is interactive. When I click on a line, I return the view from the More method FranchiseSetController - through AJAX.

Inside this perspective, there is also a table that shows all the related franchises for this franchise set. Essentially, this Master / Detail view is what it looks like:

enter image description here

There is a motive for using Bootstrap Twitter, used to display the Create form for franchises. Here's what it looks like:

<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>
      <div id="create_franchise_modal_body" class="modal-body">
        @Html.Action("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID })       
      </div>
      <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Close</a>
        <a href="#" class="btn btn-primary">Save changes</a>
      </div>
    </div>

Inside the modal body, I use the helper @Html.Actionto call the FranchiseController Create method :

@Html.Action("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID })

Here is the modal that is generated from this:

enter image description here

The Create method successfully saves the new Franchise, however I get this error:

.

LOTS StackOverflow ASP.NET , - JavaScript, " ".

, , FranchiseSet, FranchiseSet, Child - - undefined. , ?

, -, . , , , . .net mvc, .

@Html.Action ? @Html.RenderPartial, 500 Internal Server - Create method, .

Create FranchiseController - GET, POST - :

//
// GET: /Franchise/Create

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

//
// POST: /Franchise/Create

[HttpPost]
public ActionResult Create(Franchise franchise)
{
    if (ModelState.IsValid) {
        franchiseRepository.InsertOrUpdate(franchise);
        franchiseRepository.Save();
        return RedirectToAction("Index", "FranchiseSet");
    } else {
        ViewBag.PossibleFranchiseSets = franchisesetRepository.All;
        return View();
    }
}

return RedirectToAction("Index", "FranchiseSet"); @Html.Action .

!



, , @Html.RenderPartial @Html.RenderAction .

.

<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.RenderAction("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID }); }
         </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>

, .

: @{ Html.RenderAction("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID }); }

@{ } - , : Html.RenderPartial ?

FranchiseController - FranchiseSetController - . , JSON JavaScript - Razor.

, .

+5
1

:

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

GET,

//
// GET: /Franchise/Create

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

.

  • "" ,
  • @Html.Action("Create", "Franchise", new { FranchiseSetId = Model.FranchiseSetID }),
  • " " , , "",

-, , , .

+2

All Articles