Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将属性作为参数传递_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 将属性作为参数传递

C# 将属性作为参数传递,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我试图将属性从我的游戏类传递到名为竞争的方法中。我正在调用控制器中的Compete方法,但无法访问这些参数 问题:如何将UserGuess和ComputerGuess属性传递到我的Compete方法中 型号 namespace RockPaperScissors.Models { public class Game { public string UsersGuess { get; set; } public string ComputersGu

我试图将属性从我的
游戏
类传递到名为
竞争
的方法中。我正在调用控制器中的
Compete
方法,但无法访问这些参数

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

型号

namespace RockPaperScissors.Models

{
    public class Game
    {
        public string UsersGuess { get; set; }
        public string ComputersGuess { get; set; }
        public string Winner { get; set; }
    }
}
游戏逻辑

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

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

            return Outcome;
        }
    }
}
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];
        }
    }
}
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);
        }
    }
}
计算机逻辑

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

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

            return Outcome;
        }
    }
}
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];
        }
    }
}
使用系统;
使用System.Collections.Generic;
剪纸剪,生意
{
公共类计算机逻辑
{
公共字符串计算机guess()
{
var options=新列表{“石头”、“布”、“剪刀”};
var randomizer=new Random();
var random=randomizer.Next(options.Count);
返回选项[随机];
}
}
}
查看

@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)
@使用石头剪纸。模型
@模型游戏
@{
ViewBag.Title=“游戏”;
}
石头、布、剪刀
@使用(Html.BeginForm(“索引”、“游戏”))
{
输入您的决定:
@TextBoxFor(m=>m.UsersGuess,新的{@class=“formcontrol”,required=“required”})
}
计算机猜测:@Html.DisplayFor(m=>m.ComputersGuess)
获胜者:@Html.DisplayFor(m=>m.Winner)

必须首先为模型赋值

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

您还应该让
竞争
静态

将逻辑放在不同的方法中,使其过于复杂。您可以通过在模型中包含逻辑来简化此过程

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)
    }
}
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" />
}
@模型游戏
@使用(Html.BeginForm())
{
@DisplayNameFor(m=>m.UsersGuess)
foreach(Model.Options中的字符串选项)
{
@Html.RadioButtonOnce(m=>m.UsersGuess,option,新{id=”“})
@选择权
}
@Html.ValidationMessageFor(m=>m.UsersGuess)
}
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>
@模型游戏
你guessed@Model.UserGuess
电脑guessed@Model.ComputersGuess
获胜者呢is@Model.Winner
@ActionLink(“再次播放”,“播放”)

您正在尝试在创建游戏对象之前访问其属性。您有一个生成POST的表单,因此您需要一个标记有
[HttpPost]公共操作结果索引(游戏模型)
的方法,该视图中有
DisplayFor(m=>m.ComputersGuess)
@Html.DisplayFor(m=>m.Winner)
。您需要发布
UsersGuess
并重定向到另一个显示
ComputersGuess
Winner
的视图,谢谢您的回复。然而,我不确定这如何允许我将
UserGuess
ComputersGuess
传递到我的方法
Compete
@alexm,我理解你的意思,但不知道如何实现它。可以展开吗?@CodeChaser,将
UserGuess
的值发回给GET方法。在该GET方法中,您初始化
Game
的新实例,设置
UserGuess
属性,设置
ComputersGuess
(通过调用适当的方法),然后设置
Winner
属性(通过调用适当的方法),最后返回显示
UserGuess
的视图,ComputerGuess`和
获胜者
(该视图可能有一个“再次播放”链接,该链接重定向回初始方法。感谢您的回复,但是如果我从视图中获得用户输入,并且计算机值是从ComputerLogic类中随机生成的,那么这是如何工作的?太棒了,感谢您的帮助。我明天会尝试一下,如果成功,我会将其标记为答案。关于过于复杂的问题……我之所以把东西放进多个文件中,是为了促进面向对象编程;你写的一些代码不应该放在不同的类中,而不是分组在一起吗?我小小的编程冒险的目标是练习MVC和OOP,我对OOP的想法是错误的吗?不是确实如此。它的逻辑与
游戏
类特别相关。但是,你也可以使用一个方法,比如
公共无效游戏(游戏)
,来设置
计算机guess
Winner
属性。(当然,所有这些逻辑都相对简单,因此如果你想在浏览器中完成所有操作,并且避免离开页面)非常感谢@StephenMuecke,这就成功了。你介意解释一下控制器中的方法吗?我对每种方法的作用都有点困惑。不确定你不理解哪些部分。
if(!ModelState.IsValid)
检查用户是否选择了on选项(如果没有添加
ModelState
错误,因为
UsersGuess
具有
[Required]
属性),并返回带有和错误消息的视图。如果没有错误,则
返回重定向到操作(…)
重定向到
结果()
方法并将所选选项传递给它。在
Result()
方法中,初始化一个新的
游戏
,并根据传递给它的值设置
UserGuess
属性。如果(!model.Options.Contains(userChoice))行检查选择是否有效(如果有人只是导航到该方法而没有传递有效的选择-例如
/Results/Knife
)。最后调用
Play()
方法来设置
ComputerGuess
Winner
属性。