无法在视图上动态呈现标签标签

Not able to render label tag on a view dynamically

我在 .cshtml 文件中有以下代码。

<div class="row">

            <input type="text" placeholder="Enter POR ID" id="porID" class="col-md-2 input-sm" name="porTextBox">
            <button class="btn btn-primary col-md-2 btn-sm" style="margin-left:15px;width:150px;" type="submit" id="btnCreateTFSItems"><strong>Create TFS Items</strong></button>
            @if (TempData["formState"] != null)
            {
                //@Html.Label("lblsuccess", "Successfully created TFS Work Items!")
                <label id="lblsuccess" style="color:green; visibility:visible">   Successfully created TFS Work Items!</label>
            }
        </div>

并且按钮在脚本标记中调用以下函数:

<script type="text/javascript">
$(document).ready(function (e) {

    $('#btnCreateTFSItems').click(function () {
        var txt = $('#porID');
        var errorLabel = $('#lblError');
        if (txt.val() != null && txt.val() != '') {
            //alert('clicked');

            $.ajax({
                url: '@Url.Action("CreateWorkItems", "Tables")',
                type: 'POST',
                data: { 'porTextBox': $('#porID').val() }
            });
           // alert('Successfully added');
        }
        else {
            errorLabel.text("Please enter valid PORID");
            return false;
        }
    });

    $("#porID").keypress(function (e) {
        var errorLabel = $('#lblError');
        errorLabel.text("");
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });
});

问题出在 .cshtml 文件中,它正在检查条件但未添加标签。原因可能是因为页面没有刷新以呈现标签。我是 UI 开发的新手,所以我尝试了在网上找到的某些选项,但无法使其正常工作。有什么办法可以做到这一点吗?

The reason may be because page is not refreshing to render the label.

没错。您使用 JQuery(客户端脚本)向服务器发出 Ajax 请求,并期望执行 Razor 代码(服务器代码)以显示标签。这不起作用,因为服务器代码对客户端代码一无所知。

解决方案是通过JQuery显示标签。默认渲染它,但用 CSS 隐藏它(显示:none)。请求成功后,可以通过设置 CSS 显示 属性 为 "block" 或 "inline" using JQuery.

来显示标签