简体   繁体   English

如何根据用于确定要显示的按钮的值在单击之前不知道的条件动态显示按钮?

[英]How to dynamically show a button based on conditions where the value being used to determine what button to show isn't known until it's clicked?

If someone has a better title feel free to edit.如果有人有更好的标题,请随时编辑。 I inherited a project from a developer who is leaving the company and I'm scratching my head trying to find a solution to a problem the existing code provides.我从一位即将离开公司的开发人员那里继承了一个项目,我正在努力寻找现有代码提供的问题的解决方案。

Code from the view:视图中的代码:

<div>
            <table class="table">
                <tr>
                    <th class="border-bottom border-top-0">Action</th>
                </tr>
                @foreach (Step actionItem in Model.Steps)
                {
                    @if (actionItem.HasRun == false)
                    {
                        <tr class="border-top-0">
                            <td>
                                @if (actionItem.ReturnsInfo == true)
                                {
                                    <input type="button" value="Run Check" onclick="loadProcessingFeedbackPartial('@actionItem.StepID', '@Model.Client.DatabaseConnectionString' )" />
                                }
                                else
                                {
                                    <input type="submit" value="Run Check" name="btnRunStoredProcedure" asp-action="CallStepStoredProcedure" asp-route-StepID="@actionItem.StepID" asp-route-StepCompleted="@actionItem.HasRun" />
                                }
                            </td>
                        </tr>
                        break;
                    }
                }
            </table>
        </div>

Javascript being called from the button click:从按钮单击调用 Javascript:

<script type="text/javascript">
    function loadProcessingFeedbackPartial(x, y) {
        var url = '@Url.Action("ViewProcessingFeedBackPartial", "Client")';
        var stepId = x;
        var databaseConnectionString = y;
        $("#processingFeedbackPartialDiv").load(url, { stepId, databaseConnectionString },
            function () {
                $("#confirmButton").removeAttr("style");
            });
    }
</script>

Controller action:控制器动作:

public IActionResult ViewProcessingFeedBackPartial(int StepId, string DatabaseConnectionString)
    {
        FeedbackDetails feedbackDetails = new FeedbackDetails();

        feedbackDetails.Data = _clientProcessingService.GetProcessingFeedbackDetails(StepId, DatabaseConnectionString);

        return PartialView("_ViewFeedback", feedbackDetails);
    }

The button in the view has an Onclick event that goes to the Javascript function, which loads a partial view with the data from the controller calling a service method.视图中的按钮有一个 Onclick 事件,该事件转到 Javascript 函数,该函数使用来自调用服务方法的控制器的数据加载局部视图。 Here's where the problem is.这就是问题所在。 If no rows are returned, I want to bypass the partial being drawn entirely.如果没有返回行,我想绕过完全绘制的部分。

So I changed the controller action around a bit to include a condition where if the feedbackDetails.Data has 0 rows to just call a different method from the service, process as normal, but return the View instead of a partial.因此,我稍微更改了控制器操作以包含一个条件,即如果feedbackDetails.Data有 0 行,则只需从服务调用不同的方法,正常处理,但返回视图而不是部分。

public IActionResult ViewProcessingFeedBackPartial(int StepId, string DatabaseConnectionString, int ClientId)
    {
        FeedbackDetails feedbackDetails = new FeedbackDetails();

        feedbackDetails.Data = _clientProcessingService.GetProcessingFeedbackDetails(StepId, DatabaseConnectionString);
        if(feedbackDetails.Data.Rows.Count == 0)
        {
            _clientProcessingService.RunProcessStepConfirmation(DatabaseConnectionString, StepId, ClientId, "No information returned, automatically proceeding to next step.");
            return RedirectToAction("Processing", new { Id = ClientId });
        }

        return PartialView("_ViewFeedback", feedbackDetails);
    }

This "worked", except since in the view it's being called in a Javascript function that loads a partial regardless, the view is returned inside that partial instead of the view being returned.这“有效”,除了因为在视图中它是在加载部分的 Javascript 函数中调用的,否则视图将在该部分内返回,而不是返回视图。

But I'm unsure how to fix this because without first clicking the button and attempting to populate that collection with data, I don't know if it's empty (and skip the partial) or it has rows (and draw the partial).但我不确定如何解决这个问题,因为没有先点击按钮并尝试用数据填充该集合,我不知道它是空的(并跳过部分)还是它有行(并绘制部分)。

I attempted creating an intermediary controller action that returns a boolean and attempted to use the result of that inside the javascript function to either draw the partial or skip it based on the bool, but I'm not really the greatest at Javascript so I wasn't able to get it to work.我尝试创建一个返回布尔值的中间控制器操作,并尝试在 javascript 函数中使用该操作的结果来绘制部分或基于 bool 跳过它,但我并不是最擅长的 Javascript 所以我不是t 能够让它工作。

I'm unsure if the way to solve this involves creating logic that displays multiple buttons that route to different controller actions or javascript functions or just handling it all via Javascript somehow.我不确定解决这个问题的方法是否涉及创建显示多个按钮的逻辑,这些按钮路由到不同的控制器操作或 javascript 函数,或者只是通过 Javascript 以某种方式处理它。

What would be a good way to go about solving this?什么是解决这个问题的好方法?

@Mkalafut, your jQuery function is loading the controller result directly into "#processingFeedbackPartialDiv" regardless of the result received. @Mkalafut,无论收到的结果如何,您的 jQuery 函数都会将控制器结果直接加载到“#processingFeedbackPartialDiv”中。 Better to pull this initially into a variable, then add some simple logic to decide what to do next.最好先把它放到一个变量中,然后添加一些简单的逻辑来决定下一步做什么。 Potentially the controller can help by returning a null result that is easy to identify.控制器可能会通过返回一个易于识别的空结果来提供帮助。 eg例如

$.get("url", { stepId, databaseConnectionString }, function (data) {
    var result = data;
    // Some example conditional logic - adjust as required
    if (result != null){
        $("#processingFeedbackPartialDiv").html(result);
        $("#confirmButton").removeAttr("style");
    }
});

Remember, jQuery load & get are both just shorthand functions for ajax, so if needs be you can customise the code further to get the flexibility you need.请记住,jQuery 加载和获取都只是 ajax 的简写函数,因此如果需要,您可以进一步自定义代码以获得所需的灵活性。 https://api.jquery.com/jQuery.get/ https://api.jquery.com/load/ https://api.jquery.com/jQuery.get/ https://api.jquery.com/load/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未单击按钮时显示工具提示 - Show tooltip when the button isn't clicked 如何显示或隐藏<option value="s based on what is clicked from an outside radio button group?">s 基于从外部单选按钮组单击的内容?</option> - How do I show or hide the <option>s based on what is clicked from an outside radio button group? 在单击按钮之前,Javascript幻灯片演示不会显示第一个数组 - Javascript Slideshow Won't Show First Array Until the Button is Clicked 显示隐藏的表,直到单击搜索按钮 - Show hidden tables until search button clicked Python Selenium 按钮没有被点击 - Python Selenium button isn't being clicked 单击按钮后如何显示新的按钮? - How to show a new Button after Button was clicked? 基于类的按钮单击事件不会在动态创建的按钮上触发。 - Button click event based on class isn't being fired on button that is dynamically created. 使按钮在单击时禁用,但在倒计时不为零之前启用 - Make a button disable at clicked but enable until countdown isn't zero 使用on(&#39;click&#39;)函数无法识别按钮标签是否被单击? - Button tag isn't being recognized as being clicked, with on('click') function? 如何根据所用PC的已知参数使按钮重定向到特定的URL? - How do I make a button redirect to a specific url based on a known parameter of the PC being used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM