如何在 ASP .NET MVC 中将 @title.visible 更改为 false

Posted

技术标签:

【中文标题】如何在 ASP .NET MVC 中将 @title.visible 更改为 false【英文标题】:How to change the @title.visible to false in ASP .NET MVC 【发布时间】:2021-12-25 14:13:19 【问题描述】:

这是一个投注游戏,当点击卡片按钮时会翻转显示的图像卡片。我希望网站在单击卡片按钮时更改/翻转平铺图像。当我运行第一个代码循环时,如何将 Visibility 更改为 true(因为我 hv 循环代码 2 次以翻转卡片)

我的代码是

 @foreach (var tile in Model)
    
        <div style="width: 400px;">
            <div class="card" style="width: 100px; float:left;">

                @if (tile.Visible == false)
                
                    <img src="~/images/unknown.png"  style="width: 100px; height: 100px;">                    
                    <input asp-action="Index" type="submit" value="Choose" class="btn btn-primary" onclick="@tile.Visible = true;"/>
                
                else if (tile.Visible == true) //show the tile value
                
                    <img src="~/images/bust.png"  style="width: 100px; height: 100px;">
                    if (tile.Value == "0.00")
                    

                        //substract the bet coins from number of coins if lost
                    
                    else
                    

                        //increase the number of coins if won
                    
                    <input asp-controller="Game" asp-action="betCoins" type="submit" value="Choose" class="btn btn-primary" />

                

                <br>
                <div class="card-body">
                    <div>@tile.Value</div>
                </div>
            </div>
        </div>
    

【问题讨论】:

【参考方案1】:

通常,当您处理一组对象时,会定义一个唯一属性来标识集合中的每个项目。通常此属性在映射到数据库表时用作键。当需要对对象执行一些操作时,调用控制器的方法并只传递对象标识符而不传递对象本身就足够了。

假设定义如下数据模型:

public class DataObject

    public int Id  get; set; 
    public string Image  get; set; 
    public string Value  get; set; 
    public bool Visible  get; set; 

List<DataObject> model= ... // Obtain data from repository

public ActionResult Index()
   
    return View(model);



[HttpPost]
public ActionResult Choose(int tileId)

    var item = model.Where(i => i.Id == tileId).SingleOrDefault();
    item.Visible = true;
    return View("Index", model);


[HttpPost]
public ActionResult betCoins(int tileId)

    var item = model.Where(i => i.Id == tileId).SingleOrDefault();
    item.Visible = false;
    return View("Index", model);

Index.cshtml:

@model IEnumerable<Models.DataObject>

@foreach (var tile in Model)
 
<div style="width: 400px;">
    <div class="card" style="width: 100px; float:left;">

        @if (tile.Visible == false)
        
            <img src="~/images/unknown.png"  style="width: 100px; height: 100px;">            

            using (Html.BeginForm("Choose", "Game", new  tileId = tile.Id ))
                            
                <input type="submit" value="Choose" />
            
        
        else if (tile.Visible == true) //show the tile value
        
            <img src="~/images/bust.png"  style="width: 100px; height: 100px;">

            if (tile.Value == "0.00")
            
                //substract the bet coins from number of coins if lost
            
            else
            
                //increase the number of coins if won
            
            using (Html.BeginForm("BetCoins", "Game", new  tileId = tile.Id ))
            
                <input type="submit" value="Choose" />
                     
        
        <br>
        <div class="card-body">
            <div>@tile.Value</div>
        </div>
        <br>
    </div>
</div>
 

【讨论】:

以上是关于如何在 ASP .NET MVC 中将 @title.visible 更改为 false的主要内容,如果未能解决你的问题,请参考以下文章