我有一个我不明白的情况。我正在开发一个小型的网络应用程序,它可以模拟游戏池的游戏过程。我有两个作用,第一个是负责从用户收集输入的动作,第二计算所需的数据: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头生成适当的对象?
是的,我做到了。结果是一样的。 – helvy91
我不认为你可以传递那样的对象 –
@ helvy91对不起,我没有看到它是行动。您无法将对象传递给其他操作。所以解决方案要么保持sesion/tempdata/db并将其检索到另一个部分。 – Parwej