相关疑难解决方法(0)

带参数的RedirectToAction

我有一个我从锚中调用的动作,Site/Controller/Action/ID其中ID是一个int.

稍后我需要从Controller重定向到同一个Action.

有一个聪明的方法来做到这一点?目前我正在ID使用tempdata,但是当你回到f5后再次刷新页面时,tempdata就会消失,页面崩溃了.

c# asp.net-mvc controller redirecttoaction

555
推荐指数
13
解决办法
72万
查看次数

ASP.Net MVC RouteData和数组

如果我有这样的动作:

public ActionResult DoStuff(List<string> stuff)
{
   ...
   ViewData["stuff"] = stuff;
   ...
   return View();
}
Run Code Online (Sandbox Code Playgroud)

我可以使用以下URL点击它:

http://mymvcapp.com/controller/DoStuff?stuff=hello&stuff=world&stuff=foo&stuff=bar
Run Code Online (Sandbox Code Playgroud)

但在我的ViewPage中,我有这个代码:

<%= Html.ActionLink("click here", "DoMoreStuff", "MoreStuffController", new { stuff = ViewData["stuff"] }, null) %>
Run Code Online (Sandbox Code Playgroud)

不幸的是,MVC不够聪明,无法识别该动作采用数组,并展开列表以形成正确的URL路由.相反,它只是在对象上执行.ToString(),它只列出了List中的数据类型.

当目标Action的参数之一是数组或列表时,有没有办法让Html.ActionLink生成正确的URL?

- 编辑 -

正如Josh在下面指出的那样,ViewData ["stuff"]只是一个对象.我试图简化问题,但引起了一个无关的错误!我实际上使用的是专用的ViewPage <T>,因此我有一个紧密耦合的类型感知模型.ActionLink实际上看起来像:

<%= Html.ActionLink("click here", "DoMoreStuff", "MoreStuffController", new { stuff = ViewData.Model.Stuff }, null) %>
Run Code Online (Sandbox Code Playgroud)

其中ViewData.Model.Stuff被键入为List

c# arrays asp.net-mvc list asp.net-mvc-routing

24
推荐指数
1
解决办法
1万
查看次数

具有复杂深层对象的RedirectToAction(..)失败

我正在尝试将一个对象从一个控制器动作传递给另一个.我传递的对象或多或少看起来像这样:

public class Person
{
   public string Name { get; set; }
   public List<PhoneNumber> PhoneNumbers {get; set; }
   public List<Address> Addresses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器看起来像这样:

public class DialogController : Controller
{
    public ActionResult Index()
    {
        // Complex object structure created
        Person person = new Person();
        person.PhoneNumbers = new List();
        person.PhoneNumbers.Add("12341324");

        return RedirectToAction("Result", "Dialog", person);

    }

    public ActionResult Result(Person person)
    {
        string number = person.PhoneNumbers[0].ToString();
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果方法因空指针异常而失败,因为在使用RedirectToAction()方法调用Result操作后,PhoneNumbers列表突然为null.

以前有没有人见过这种行为?

干杯,

彼得

asp.net-mvc

9
推荐指数
2
解决办法
6960
查看次数