c# - 将模型从一个 Controller 传递到 MVC3 中的另一个 Controller 不起作用

标签 c# asp.net asp.net-mvc-3

我正在将一个模型从一个 Controller 方法传递到另一个 Controller 方法,以将所有值收集到模型字段。我正在做如下所示

[HttpPost]
    public ActionResult A(Model m, string s)
    {
        if (ModelState.IsValid) 
        {
            if (m.l == null || m.k == null)
            {
                //Do something.

            }
            else
                return View("B", m); // this is where by debug point comes...
        }

        return View(m);


    }


public ActionResult B(Model m)
    {

        return View(m);
    }

[HttpPost]
    public ActionResult B(Model m, string s)
    {
        if (ModelState.IsValid)
        {
            if (m.p == null || m.j == null)
            {
                //do something
            }
            else
            {
               // do something and redirect to somewhere else 
            }


        }

但正如我所展示的,调试点如下所示。

return View("B", m);

这应该命中 Controller 方法“B”但问题是它没有命中 Controller 方法“B”。但它显示了 Controller 方法“B”的 View 所以,我对这个问题感到困惑。而且我在方法“B”的 httppost 中看不到 (l,k) 的值。这是什么原因。

我想知道,我这样做是对还是错。如果我做错了,请你解释一下。擅长MVC的人可以帮助我吗? 提前致谢。

最佳答案

那段代码——return View("B", m)——实际上并没有重定向到action B,它只是呈现 View B 与给定的模型。如果你想执行这个 Action ,那么你应该使用:

return B(m);

关于c# - 将模型从一个 Controller 传递到 MVC3 中的另一个 Controller 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12066953/

相关文章:

c# - Excel VSTO 2010 - 显示选择字段的对话框

c# - 如何从对象列表的属性中提取不同的对象?

asp.net - 如何阻止我的 ASP.NET Web 服务返回 xml 数据?

asp.net - 检测并显示 IE8,然后使用 MVC 3 Razor 显示现有的 IE8 css 样式表

asp.net-mvc-3 - 急需的 ASP.NET MVC 3 教程

c# - .NET 流,在对象之间传递流,最佳实践 (C#)

C#在可调整大小的图片中绘制点

asp.net - HTMLEditorExtender 在异步回发时对 img 标签进行编码

asp.net - IIS7.5 上的静态文件处理程序不提供脚本

c# - MVC-3 是否包含用于快速自动撰写丰富 (HTML) 电子邮件的库?