2017-07-27 43 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)); 
} 
相關問題