ASP.NET MVC 4 - 使用儿童行动模态保存后重定向 [英] ASP.NET MVC 4 - Redirect after Modal Save using Child Action

查看:141
本文介绍了ASP.NET MVC 4 - 使用儿童行动模态保存后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 - 有关于儿童行动很多帖子都不允许执行重定向SO和ASP.NET论坛操作。下面的解决方案似乎是我见过的这个问题,最简单的解决方法。如果你有这个问题 - 希望你会发现它有帮助。如果有一个更好的,我当然愿意听听建议。

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 solution below seems to be the simplest workaround that I've seen yet for this problem. If you have this issue - hopefully you'll find it helpful. If there is a better one, I'm certainly open to suggestions.

我一直令人头大我与这脑子整天...

I've been wracking my brain with this all day...

我有一个 FranchiseSet 模式和加盟模型 - 的关系是一个FranchiseSet具有一对多的专营权

I have a FranchiseSet model and a Franchise model - the relationship is that a FranchiseSet has-many Franchises.

在所述FranchiseSet索引视图有特权集,其中每一行是可点击的一个表。当我点击一排,我从详情的方法返回一个视图中的 FranchiseSetController - 通过AJAX

On the FranchiseSet index view there is a table of Franchise Sets where each row is clickable. When I click a row, I return a view from the Details method of the FranchiseSetController - through AJAX.

在这种观点是有也显示为特许经营集合所有相关特许经营的表。从本质上讲,它是一个主/详细信息查看 - 这里是什么样子:

Within that view there is a also a table that shows all related Franchises for that Franchise set. Essentially, it is a Master/Detail View - Here is what that looks like:

有是正在使用Twitter的引导模式来显示特权创建表单。下面是一个样子:

There is a Twitter Bootstrap modal being used to display the Create form for franchises. Here's what that 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>

模态的身体里面,我使用的是 @ Html.Action 助手致电 FranchiseController的创建方式:

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

下面是从生成该模态:

Create方法成功地保存新的特权,但是,我得到这个错误:

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

儿童的行为是不允许进行重定向操作。

我见过的地段在计算器上的帖子和ASP.NET论坛在这个问题上,我一直试图实施一些解决方案 - 即使使用JavaScript重定向,但我还是避风港'T之所以能得到这个工作。

I've seen LOTS of posts on StackOverflow and the ASP.NET Forums on this issue, and I've tried to implement some of the solutions - even using JavaScript redirects, but I still haven't been able to get this to work.

我明白,因为我已经是FranchiseSet控制器里面,它已经在试图呈现基于FranchiseSet模型视图,因此子视图 - 特许经营模式的 - 是不确定的。那么,什么是处理中父母与子女互动的行动的理想方式?

I understand that since I am already inside of the FranchiseSet Controller, that it is already trying to render a view based on the FranchiseSet model, so the Child view - of the Franchise model - is undefined. So what is the ideal way to deal with interactions among parent and child actions?

这似乎是没有通用的解决方案非常普遍的问题。或者至少不是一个我能找到。我是比较新的.NET MVC所以这可能只是我的。

This seems to be a very common problem without a common solution. Or at least not one that I can find. I'm relatively new to .net mvc so it may just be me.

中使用 @ Html.Action 一个糟糕的设计选择在这种情况下?我曾尝试使用 @ Html.RenderPartial 但导致500内部服务器错误 - 即使我有创建方法设置返回一个局部视图

Is using @Html.Action a bad design choice in this scenario? I have tried to use @Html.RenderPartial but that causes a 500 Internal Server error - even if I have the Create method setup to return a partial view.

下面是FranchiseController创建行动 - GET和POST - 我使用:

Here are the FranchiseController Create actions - both GET and POST - that I am using:

//
// 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();
    }
}

返回RedirectToAction(指数,FranchiseSet);会同 @ Html.Action 从视图似乎是问题的根源。

The return RedirectToAction("Index", "FranchiseSet"); in conjunction with the @Html.Action from the view seems to be the source of the problem.

任何意见得多AP preciated!

SOLUTION

我用修改后的模态code罗素以下建议,但是我切换了@ Html.RenderPartial方法为code块中的@ Html.RenderAction方法。

I used the modified modal code that Russell suggested below, however I switched the @Html.RenderPartial method for the @Html.RenderAction method within a code block.

下面是什么样子了。

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

我还修改了控制器的创建方法像罗素以上建议。

I also modified the controller's Create method like Russell suggested above.

使用这个: @ {Html.RenderAction(创建,特许经营,新的{FranchiseSetId = Model.FranchiseSetID}); }

我把它,因为什么,我在这里学到了 @ {} 标签中:<一href=\"http://stackoverflow.com/questions/5435893/html-renderpartial-giving-me-strange-overload-error\">Html.RenderPartial让我奇怪的超载错误?

I'm putting it within the @{ } tags because of what I learned here: Html.RenderPartial giving me strange overload error?

我能够得到FranchiseController重定向回其母公司 - 中FranchiseSetController的指数方法 - 没有问题。再加上它并不需要我回去JSON和尝试做一个JavaScript重定向 - 这是所有的Razor视图code

I was able to get the FranchiseController to redirect back to its parent - the FranchiseSetController's Index method - with no problems. Plus it didn't require me to return JSON and try to do a JavaScript redirect - it's all Razor view code.

我希望这是解决了如何从儿童操作回的

I hope this is solves the problem of how to Redirect from Child actions back to the Parent.

推荐答案

试试这个为你的模式:

<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方法,因此它返回一个局部视图结果

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. 您的创建的观点应该是有你的所有表单输入的局部视图

  2. 当你调用 @ Html.Action(创建,特许经营,新的{FranchiseSetId = Model.FranchiseSetID})你是想渲染局部视图

  3. 您正在想的模式底部的保存更改链接实际保存您的模式,而不是所显示的创建按钮

  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.

这篇关于ASP.NET MVC 4 - 使用儿童行动模态保存后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆