相关疑难解决方法(0)

带参数的RedirectToAction

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

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

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

c# asp.net-mvc controller redirecttoaction

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

具有复杂深层对象的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
查看次数

RedirectToAction到另一个Controller并传递参数

我是C#的新手,特别是在ASP.NET MVC中.

我有我的HomeController,它包含这个方法:

public ActionResult Error(Error error)
{
    return View(error);
}
Run Code Online (Sandbox Code Playgroud)

现在我有另一个Controller,里面有以下行:

return RedirectToAction("Error","Home", new { Error = (new Error("ErrorName","ErrorDescription"))} );
Run Code Online (Sandbox Code Playgroud)

您可能会注意到,我正在尝试将Error对象传递给另一个控制器,然后该控制器应将其传递给View.

我自己编写的Error类,没什么了不起的:

public class Error
{
    public String name {  get; private set; }
    public String description {  get; private set; }
    public int number {  get; private set; }

    public Error(String name, String description)
    {
        this.name = name;
        this.description = description;
        number = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,每当我尝试访问HomeController中的错误变量时,它就是null.我已经用谷歌搜索了一些帖子,但我不明白,为什么我的代码不起作用.没有错误,只有这个空值对象..我感谢任何帮助!:)

c# asp.net asp.net-mvc asp.net-mvc-4

6
推荐指数
1
解决办法
9560
查看次数

使用T4MVC在RedirectToAction()中传递对象

可能重复:
将数据从Action传递给另一个Action

我有一个视图,我提交一个表单,并根据我想重定向到一个动作的结果.对应于和动作的视图是强类型的,它应该接受ResultsViewModel.

我正在尝试使用T4MVC传递ResultsViewModel.

以下是代码:

    [HttpPost]
    public virtual ActionResult AddEntity(string viewModel)
    {
        //Deserialize using Json.NET
        var entity = JsonConvert.DeserializeObject<MyEntity>(viewModel);

        var success = DoSomething(); //returns boolean
        if(success)
        {
            var result = new ResultsViewModel { MyEntity = entity, MessageId = 1};
            return RedirectToAction(MVC.MyController.ResultsPage(result));
        }

        var result = new ResultsViewModel { MyEntity = entity, MessageId = 2};
        return RedirectToAction(MVC.MyController.ResultsPage(result));
    }

    public virtual ActionResult ResultsPage(ResultsViewModel viewModel)
    {
        return View(viewModel);
    }
Run Code Online (Sandbox Code Playgroud)

当代码到达时

    public virtual ActionResult ResultsPage(ResultsViewModel viewModel)
    {
        return View(viewModel);
    }
Run Code Online (Sandbox Code Playgroud)

viewModel始终等于null.

我知道我可以这样做:

return RedirectToAction("ResultsPage", …
Run Code Online (Sandbox Code Playgroud)

c# t4mvc asp.net-mvc-3

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