我正在将一个模型从一个 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/