简体   繁体   English

在ASP.NET MVC中单击按钮后更新视图

[英]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.

相关问题 单击按钮后在视图中加载数据,而无需重新加载页面asp.net mvc中的所有数据 - Loading data in view after click the button without reload all data in page asp.net mvc 在ASP.NET MVC中单击按钮时呈现部分视图 - Rendering partial view on button click in ASP.NET MVC 我在ASP.NET MVC的视图中有Button和TextBox-单击此按钮后如何将Button的值发送到TextBox? - I have Button and TextBox in View of ASP.NET MVC - how to send value of Button to TextBox after Click on this Button? 更新 ASP.NET MVC 视图中的变量 - updating a variable in an ASP.NET MVC view 打开部分视图按钮,单击内部的模态asp.net MVC? - Open Partial View on button click inside a modal asp.net mvc? 如何在按钮单击并验证后调用使用ajax的asp.net mvc表单提交事件? - How to call asp.net mvc form submit event which use ajax after button click and validate? 如何防止用户在启用了缓存的asp.net mvc中注销后单击返回按钮 - How to prevent user to click on the back button after logout in asp.net mvc with cache enabled 视图中的ASP.Net MVC动态文本未更新页面 - ASP.Net MVC dynamic text in view not updating page 在ASP.NET MVC C#中使用Jquery更新部分视图 - Updating partial view with Jquery in ASP.NET MVC C# ASP.NET MVC剃刀-单击DropDownList重定向到另一个视图 - ASP.NET MVC Razor - Click on a DropDownList to redirect to another view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM