将属性作为参数传递

时间:2015-09-12 01:12:00

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

我试图将Game类中的属性传递给名为Compete的方法。我在控制器中调用Compete方法,但无法访问这些参数。

  

问题:如何将UserGuessComputerGuess属性传递到我的Compete方法

模型

namespace RockPaperScissors.Models

{
    public class Game
    {
        public string UsersGuess { get; set; }
        public string ComputersGuess { get; set; }
        public string Winner { get; set; }
    }
}

GameLogic

namespace RockPaperScissors.Business
{
    public class GameLogic
    {
        public string Outcome { get; set; }

        public string Compete(string userInput, string computerInput)
        {
           // Logic Here

            return Outcome;
        }
    }
}

GameController

using System.Web.Mvc;
using RockPaperScissors.Business;
using RockPaperScissors.Models;

namespace RockPaperScissors.Controllers
{
    public class GameController : Controller
    {
        public ActionResult Index()
        {

            var model = new Game
             {
                 Winner = new GameLogic().Compete(UserGuess, ComputerGuess)
             };

            return View(model);
        }
    }
}

ComputerLogic

using System;
using System.Collections.Generic;

namespace RockPaperScissors.Business
{
    public class ComputerLogic
    {
        public string ComputersGuess()
        {
            var options = new List<string> { "Rock", "Paper", "Scissors" };

            var randomizer = new Random();
            var random = randomizer.Next(options.Count);

            return options[random];
        }
    }
}

查看

@using RockPaperScissors.Models
@model Game

@{
    ViewBag.Title = "Games";
}

<h2>Rock, Paper, Scissors</h2>

@using (Html.BeginForm("Index", "Game"))
{
    <div class="form-group">
        <label>Enter your decision:</label>
        @Html.TextBoxFor(m => m.UsersGuess, new { @class = "form-control", required = "required" })
    </div>
    <div>
        <input class="btn btn-primary" type="submit" value="Guess" />
    </div>
}

<h3>Computers Guess: </h3> @Html.DisplayFor(m => m.ComputersGuess)

<h2>Winner: </h2> @Html.DisplayFor(m => m.Winner)

2 个答案:

答案 0 :(得分:1)

你在单独的方法中使用逻辑会让它复杂化。您可以通过在模型中包含逻辑来简化此操作

public class Game
{
    public Game()
    {
      Options = new List<string>(){ "Rock", "Paper", "Scissors" };
    }
    [Display(Name = "Select you guess")]
    [Required]
    public string UsersGuess { get; set; }
    public string ComputersGuess { get; set; }
    public string Winner { get; set; }
    public List<string> Options { get; private set; }
    public void Play()
    {
      var random = new Random().Next(Options .Count);
      ComputersGuess = Options[random];
      Winner = // your logic here (compare UserGuess and ComputerGuess)
    }
}

然后创建单独的方法来显示用户选择的表单,以及显示结果

public ActionResult Play()
{
  Game model = new Game();
  return View(model);
}
[HttpPost]
public ActionResult Play(Game model)
{
  if (!ModelState.IsValid)
  {
    return View(model);
  }
  return RedirectToAction("Result", new { userChoice = model.UserGuess });
}
public ActionResult Result(string userChoice)
{
  Game model = new Game() { UsersGuess = userChoice };
  if (!model.Options.Contains(userChoice))
  {
    return new HttpResponseMessage(HttpStatusCode.BadRequest, "Your error message here");
  }
  model.Play();
  return View(model);
}

Play.cshtml

@model Game
@using (Html.BeginForm())
{
  <div>@Html.DisplayNameFor(m =>m.UsersGuess)</div>
  foreach(string option in Model.Options)
  {
    <label>
      @Html.RadioButtonFor(m => m.UsersGuess, option, new { id = "" })
      <span>@option</span>
    </label>
  }
  @Html.ValidationMessageFor(m => m.UsersGuess)
  <input type="submit" value="Play" />
}

Result.cshtml

@model Game
<div><span>You guessed</span><span>@Model.UserGuess</span></div>
<div><span>The computer guessed</span><span>@Model.ComputersGuess</span></div>
<div><span>And the winner is</span><span>@Model.Winner</span></div>
<div>@Html.ActionLink("Play again", "Play")</div>

答案 1 :(得分:0)

您必须先为模型指定值。

var model = new Game()
{
ComputersGuess = "someVal",
UserGuess = "anotherVal",
Winner = new GameLogic().Compete(UserGuess, ComputerGuess)
};

您还应该Compete static