2

我试图根据 IF 条件在我的页面中显示特定的 HTML,我有一个静态变量来确定页面的状态和显示的内容,问题是在第一个页面加载后,不再呈现 html。

这是我的一个 if :

   @if (WhiteLabel.Controllers.GlobalVariables.Step == 2)
            {
                <text>
            <div id="thirdDiv" data-step="3" class="no-show">
                <div class="span6">

我有一个按钮来更改全局变量的值,例如:

<form  action="UploadPhoto/setNext" method="post">
                                <button class="btn" type="submit">siguiente</button>
                            </form>

它进入了这个控制器的方法:

public ActionResult setNext()
        {
            if (GlobalVariables.Step < 3)
            {
                GlobalVariables.Step  += 1;
            }
            else
            {
                RedirectToAction("AdjustDots", "VirtualTesterController");
            }
            return View("Load");
        }

更新完整代码:

    @if (WhiteLabel.Controllers.GlobalVariables.Step == 1)
    {
   <div id="firstDiv" data-step="1" >
        <!-- SUBIR FOTO -->
        <div class="row-fluid">
            <div class="span3">
                <div class="md-your-data">
                    <div class="hd">
                    <h2>1. @ViewBag.Strings["completarDatos"]</h2>
                    </div>
                    <div class="bd">
                        <ul>
                            <li>
                                <div class='selectBox'>
                                <span class='selected' id="ageSelected">@ViewBag.Strings["rangoEdad"]</span>
                                    <span class='selectArrow'></span>
                                    <div class="selectOptions">
                                        <span class="selectOption" value="Option 1">Option 1</span>
                                        <span class="selectOption" value="Option 2">Option 2</span>
                                        <span class="selectOption" value="Option 3">Option 3</span>
                                    </div>
                                </div>
                            </li>
                            <li>
                                <div class='selectBox'>
                                <span class='selected' id="skinToneSelected">@ViewBag.Strings["tonoPiel"]</span>
                                    <span class='selectArrow'></span>
                                    <div class="selectOptions">
                                        <span class="selectOption" value="Option 1">Option 1</span>
                                        <span class="selectOption" value="Option 2">Option 2</span>
                                        <span class="selectOption" value="Option 3">Option 3</span>
                                    </div>
                                </div>
                            </li>
                            <li>
                                <div class='selectBox'>
                                <span class='selected' id="skinTypeSelected">@ViewBag.Strings["tipoPiel"]</span>
                                    <span class='selectArrow'></span>
                                    <div class="selectOptions">
                                        <span class="selectOption" value="Option 1">Option 1</span>
                                        <span class="selectOption" value="Option 2">Option 2</span>
                                        <span class="selectOption" value="Option 3">Option 3</span>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="ft">
                    </div>
                </div>
            </div>

            <div class="span3">
                <div class="md-upload-photo">
                    <div class="hd">
                    <h2>2. @ViewBag.Strings["SubiTuFoto"]</h2>
                    </div>
                    <div id="pictureDiv" class="bd">
                        <div id="dropPicture" class="photo-cont no-photo" ondragover="allowDrop(event)" ondrop="onDrop(event)">
                            <div id="picDiv"></div>
                        </div>

                    </div>
                    <div class="ft">
                        <div class="file-input-wrapper">
                            <button class="btn-file-input">@ViewBag.Strings["subir"]</button>
                            <input id='file' class="btn-file" type='file' accept="image/*" onchange="manageFilePicker(event)" />
                        </div>

                        @using (Html.BeginForm("setNext", "UploadPicture"))
                        {
                             <button class="btn"  type="submit">Siguiente</button>
                        }

                    </div>
                </div>
            </div>

            <div class="span6">
                <div class="md-model-photo">
                    <div class="hd">
                    <h2>@ViewBag.Strings["Elegirfotodemodelo"] </h2>
                    </div>
                    <div class="bd">
                        <ul id="modelsList">
                            <li>
                                <img src="~/Content/images/imagen-modelo-upload.png" alt="" />
                            </li>
                            <li>
                                <img src="~/Content/images/imagen-modelo-upload.png" />
                                <img src="~/Content/images/imagen-modelo-upload.png" alt="" />
                            </li>
                            <li>
                                <img src="~/Content/images/imagen-modelo-upload.png" alt="" />
                            </li>
                            <li>
                                <img src="~/Content/images/imagen-modelo-upload.png" alt="" />
                            </li>
                        </ul>
                    </div>
                    <div class="ft">
                    </div>
                </div>
            </div>

        </div>
        <!-- END SUBIR FOTO -->
       </div>
    }


      @if (WhiteLabel.Controllers.GlobalVariables.Step > 2)
    {
    <div class="row-fluid">
        <!-- BEGIN MANAGE PHOTO -->

        <div id="secondDiv" data-step="2" class="no-show">
            <div class="span6">
                <div class="md-adjust-photo-img">
                    <div class="bd">
                        <div class="img-cont">
                            <img src="~/Content/images/img-modelo.png" alt="" />
                        </div>
                    </div>
                </div>
            </div>
             @if (WhiteLabel.Controllers.GlobalVariables.Step == 2)
             {
            <div id="thirdDiv" data-step="3" class="no-show">
                <div class="span6">
                    <div class="md-adjust-photo-data step-1">
                        <div class="hd">
                        <h3>4. @ViewBag.Strings["encuadrarFoto"]</h3>
                        <p>@ViewBag.Strings["encuadrarFotoDesc"]</p>
                        </div>
                        <div class="bd">
                        </div>
                        <div class="ft">
                            @using (Html.BeginForm("setPrevious", "UploadPicture"))
                            {
                                <button class="btn" type="submit">Atras</button>
                            }
                            @using (Html.BeginForm("setNext", "UploadPicture"))
                            {
                                <button class="btn" type="submit">Siguiente</button>
                            }
                        </div>
                    </div>
                </div>
            </div>
             }
             @if (WhiteLabel.Controllers.GlobalVariables.Step == 3)
             {
            <div id="fourthDiv" data-step="4" class="no-show">
                <div class="span6">
                    <div class="md-adjust-photo-data step-2">
                        <div class="hd">
                <h3>5. @ViewBag.Strings["brilloYcontraste"]</h3>
                <p>@ViewBag.Strings["brilloYcontrasteDesc"]</p>
                        </div>
                        <div class="bd">
                            <div class="col">
                    <span>@ViewBag.Strings["brightness"]</span>
                                <div class="tone-bar">
                                    <span></span>
                                </div>
                            </div>
                            <div class="col">
                    <span>@ViewBag.Strings["contrast"]</span>
                                <div class="tone-bar">
                                    <span></span>
                                </div>
                            </div>
                        </div>
                        <div class="ft">
                            @using (Html.BeginForm("setPrevious", "UploadPicture"))
                            {
                                <button class="btn" type="submit">Atras</button>
                        }
                            @using (Html.BeginForm("setNext", "UploadPicture"))
                            {
                                <button class="btn" type="submit">Siguiente</button>
                        }
                        </div>
                    </div>
                </div>
            </div>
             }
        </div>
    </div>

    }
4

2 回答 2

2

首先,我建议您对选择性HTML 使用部分视图 - 它会清理您的代码。所以想象这就是我在这里为你做的,这纯粹是给你逻辑

@if (WhiteLabel.Controllers.GlobalVariables.Step == 1)
{
    @Html.RenderPartial("Step1")
}

@if (WhiteLabel.Controllers.GlobalVariables.Step > 2)
{
    // some default HTML here
    @if (WhiteLabel.Controllers.GlobalVariables.Step == 2)
    {
        @Html.RenderPartial("Step2")
    }
    @if (WhiteLabel.Controllers.GlobalVariables.Step == 3)
    {
        @Html.RenderPartial("Step3")
    }
}

因此,纯粹从逻辑 POV 来看,因为没有输出Step需要> 3。鉴于您正在使用static变量,发生这种情况的可能性是合理的,因为您有所谓的竞争条件。如果 2 个线程(即 2 个会话)尝试继续到下一页,则两者都将尝试增加Step- 竞争条件出现的地方是

// Assume 'Step' is 2 here
// Session A checks first, then Session B - both checks pass
if (GlobalVariables.Step < 3)
{
    // Session A increments first, then Session B
    GlobalVariables.Step += 1; // value is now 4
}

最重要的是,您的应用程序变得独立于会话,即如果 1 个用户在第 3 步,那么其他人也是如此!

我建议将您的Step财产存储在 users 中Session

于 2013-09-30T14:34:19.777 回答
-1

这是行不通的。该变量不会持续存在,因为控制器会在每次回发时重建。你有一些选择,但我认为最好的是这个;将值存储在其中,Session以便可以在回发期间检索它,并且它特定于该用户的会话。

因此,在您的控制器中,您可能会执行以下操作:

var step = Session["step"] == null ? 1 : (int)Session["step"];
step++;
this.ViewBag.Step = step;
Session["step"] = step;

现在在您看来,不是检查静态属性,而是利用ViewBag

(int)ViewBag.Step == 1

例如。事实上,因为它是dynamic,你甚至不必强制转换:

ViewBag.Step == 1

或者可能:

ViewBag.Step < 3
于 2013-09-30T14:08:11.403 回答