将属性作为参数传递

本文关键字:参数传递 属性 | 更新日期: 2023-09-27 18:12:20

我试图将属性从我的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];
        }
    }
}
<<p> 视图/strong>
@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)

将属性作为参数传递

您将逻辑置于单独的方法中,使其过于复杂。您可以通过在模型

中包含逻辑来简化这一点
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>

你必须首先给你的模型赋值。

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

你也应该把Compete改成static .