[英]Updating a view after button click in ASP.NET MVC
Have had a look at the similar questions on here but can't seem to find a solution. 看过这里的类似问题,但似乎找不到解决方法。 I'm very new to MVC so would appreciate any help. 我是MVC的新手,因此不胜感激。
I've been trying to build a simple browser game where the user clicks to attack, the damage is calculated and the updated stats for the characters are updated in the view. 我一直在尝试构建一个简单的浏览器游戏,在该游戏中,用户单击进行攻击,计算损坏程度,并在视图中更新角色的更新统计信息。 My main problem is getting the view to update with the new HitPoint values for the characters after the user has clicked the Attack button. 我的主要问题是在用户单击“攻击”按钮后,使用角色的新HitPoint值更新视图。
I've had a number of errors as I tried to get it working, but most recently am getting a Null Reference Exception. 我尝试使其正常运行时遇到了许多错误,但最近却收到了Null Reference Exception。 Not sure why 不知道为什么
Here is my code: 这是我的代码:
Controller 调节器
public ViewResult CharacterSelect()
{
var heroes = _context.Heroes.ToList();
return View(heroes);
}
public ActionResult BattleArena(int? id)
{
Random random = new Random();
var monsterId = random.Next(8);
Monster arenaMonster = _context.Monsters.SingleOrDefault(m => m.Id == monsterId);
Hero arenaHero = _context.Heroes.SingleOrDefault(m => m.Id == id);
var battleViewModel = new BattleViewModel(arenaHero, arenaMonster);
return View(battleViewModel);
}
[HttpPost]
public ActionResult Attack(int? id, int? mid)
{
var hero = _context.Heroes.SingleOrDefault(m => m.Id == id);
var monster = _context.Monsters.SingleOrDefault(m => m.Id == id);
var attackBattleViewModel = new BattleViewModel(hero, monster);
if (attackBattleViewModel != null)
{
Dice newDice = new Dice();
int heroDamage = attackBattleViewModel.Hero.AttackRoll(newDice);
attackBattleViewModel.Monster.Defend(heroDamage);
int monsterDamage = attackBattleViewModel.Monster.AttackRoll(newDice);
attackBattleViewModel.Hero.Defend(monsterDamage);
return View(attackBattleViewModel);
}
else
{
return RedirectToAction("Index");
}
}
View 视图
<div class="arenaRow">
<div class="container">
<div class="row arenaHealth">
<div class="col-md-4">
<p class="arenaHp">
@Model.Hero.HitPoints
</p>
</div>
<div class="col-md-4">
@using (Html.BeginForm("Attack", "Battle", FormMethod.Post))
{
<button type="submit" class="attackButton" onclick="location.href='@Url.Action("Attack", "Battle", new { id = @Model.Hero.Id, mid = @Model.Monster.Id })'">Attack</button>
}
</div>
<div class="col-md-4">
<p class="arenaHp">
@Model.Monster.HitPoints
</p>
</div>
</div>
</div>
</div>
BattleViewModel 战斗模型
public class BattleViewModel
{
public Monster Monster { get; set; }
public Hero Hero { get; set; }
public BattleViewModel (Hero hero, Monster monster)
{
Hero = hero;
Monster = monster;
}
}
Again, any help for the rookie would be greatly appreciated. 同样,对于菜鸟的任何帮助将不胜感激。
In your Post Attack method the problem is this line of code: 在您的Post Attack方法中,问题是以下代码行:
if (attackBattleViewModel != null)
In the linie above you have declared this as New BattleViewModel So thhe test will always be not null. 在上面的linie中,您已将其声明为New BattleViewModel,因此test始终不会为null。 Once in here, your trying to use properties of the hero and monster objects which may not have been instantiated. 进入此处后,您将尝试使用可能尚未实例化的英雄和怪物对象的属性。 It might be better to change the above line to : if (hero != null && monster != null)
otherwise you need to handle for null on these in the function 最好将上面的行更改为: if (hero != null && monster != null)
否则,您需要在函数中处理这些值的null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.