2017-07-27 124 views
1

我有一个我不明白的情况。我正在开发一个小型的网络应用程序,它可以模拟游戏池的游戏过程。我有两个作用,第一个是负责从用户收集输入的动作,第二计算所需的数据:ASP.NET MVC 5 - 自定义类型的RedirectToAction传递空对象

[HttpPost] 
public ActionResult UserInput(UserInputViewModel inputParameters) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(); 
    } 

    return RedirectToAction("Play", new { inputParameters }); 
} 

public ActionResult Play(UserInputViewModel playParameters) 
{ 
    PocketName resultPocketName; 
    IEnumerable<Point> crossPoints; 

    PoolTable poolTable = new PoolTable((int)playParameters.Width, (int)playParameters.Height, (int)playParameters.BallPointX, (int)playParameters.BallPointY, playParameters.VectorX, playParameters.VectorY); 
    resultPocketName = poolTable.Play(); 
    crossPoints = poolTable.CrossPoints; 

    ViewBag.ResultPocketName = resultPocketName; 
    ViewBag.CrossPoints = crossPoints; 

    return View(); 
} 

而且UserInputViewModel看起来是这样的:

public class UserInputViewModel 
{ 
    [Required(ErrorMessage = "Please specify width.")] 
    [ProperWidth(ErrorMessage = "Width must be an even number.")] 
    [Range(300, 700)] 
    public uint Width { get; set; } 

    [Required(ErrorMessage = "Please specify height.")] 
    [Range(150, 500)] 
    public uint Height { get; set; } 

    [Required(ErrorMessage = "Please specify ball position X.")] 
    [Display(Name = "Ball position X")] 
    [ProperBallPosition("Width", ErrorMessage = "Ball position X cannot be equal or higher than pool table width.")] 
    public uint BallPointX { get; set; } 

    [Required(ErrorMessage = "Please specify ball position Y.")] 
    [Display(Name = "Ball position Y")] 
    [ProperBallPosition("Height", ErrorMessage = "Ball position Y cannot be equal or higher than pool table width.")] 
    public uint BallPointY { get; set; } 

    [Required(ErrorMessage = "Please specify vector X.")] 
    [Display(Name = "Vector X value")] 
    [Range(-1000, 1000)] 
    public int VectorX { get; set; } 

    [Required(ErrorMessage = "Please specify vector Y.")] 
    [Display(Name = "Vector Y value")] 
    [Range(-1000, 1000)] 
    public int VectorY { get; set; } 
} 

正如你看到的我是路过自定义类型(viewmodel)从UserInput()动作到Play()动作。 UserInput()操作中的inputParameter变量具有适当的值,但当程序转到Play()操作时,它为空或为空(包含在对象中的类型的默认值)。

据我了解,默认的ASP.NET模型绑定验证自定义对象需要什么属性并在客户端发送的http头中搜索它们。我坚持使用标准的ASP.NET验证模式,所以我不明白为什么我的应用程序在将HTTP标头参数转换为.NET对象时存在问题。当我用预定义类型(即字符串)替换自定义类型时,所有内容都应该是。

我的问题是:为什么ASP不能在这种情况下从http头生成适当的对象?

+0

是的,我做到了。结果是一样的。 – helvy91

+1

我不认为你可以传递那样的对象 –

+0

@ helvy91对不起,我没有看到它是行动。您无法将对象传递给其他操作。所以解决方案要么保持sesion/tempdata/db并将其检索到另一个部分。 – Parwej

回答

0

也许尝试这样的事情

return RedirectToAction("Play", "ControllerName", inputParameters); 

也可能你不只是做了在Play完成UserInput ActionResult里面,然后就返回什么都查看您打算在Play ActionResult返回计算?

[HttpPost] 
public ActionResult UserInput(UserInputViewModel inputParameters) 
{ 
    if (!ModelState.IsValid)return View(); 

    PocketName resultPocketName; 
    IEnumerable<Point> crossPoints; 

    PoolTable poolTable = new PoolTable((int)inputParameters.Width, (int)inputParameters.Height, (int)inputParameters.BallPointX, (int)inputParameters.BallPointY, inputParameters.VectorX, inputParameters.VectorY); 
    resultPocketName = poolTable.Play(); 
    crossPoints = poolTable.CrossPoints; 

    ViewBag.ResultPocketName = resultPocketName; 
    ViewBag.CrossPoints = crossPoints; 

    return View("ViewName", whatEverModelYouNeed); 
} 
0

用途:

return Play(inputParameters); 

,而不是

return RedirectToAction("Play", new { inputParameters }); 

- 编辑

好知道,你不能传递对象获取方法,这是不可能的要么使用POST,要么RedirectToAction将302返回给浏览器,这意味着浏览器将发出GET请求!

我上面写道会做的伎俩解决方案,但它哈克,不推荐:)

0

谢谢你们的帮助,但我刚刚找到最满意的答案自己:)。我使用了RouteValueDictionary()。 UserInput()动作应该如下所示:

[HttpPost] 
public ActionResult UserInput(UserInputViewModel inputParameters) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(); 
    } 

    return RedirectToAction("Play", "Home" , new RouteValueDictionary(inputParameters)); 
}