jquery - Return PartialView ("view", model) 与 Ajax 正在更新标签,但不是文本框,即使对于相同的属性

标签 jquery ajax asp.net-mvc vb.net asp.net-mvc-5

我正在使用 MVC 5、VB.NET、jQuery。
在我的一个页面上,我正在使用 ajax 调用调用一个操作,并尝试根据结果重新加载 html。
我正在修改两个属性:

  • 留言
  • 标识符

  • 消息正在被预期结果替换,但是标识符不是。
    这是代码:
    看法:
    @model CapitalLending.LoanScoreModel
        
    
    <div class="container">
        <form id="scoring-form">
            <label class="text-danger">@Html.DisplayFor(model => model.ErrorMessage)</label>
            <label class="text-success">@Html.DisplayFor(model => model.SuccessMessage)</label>
            @Html.HiddenFor(model=>model.LoanId)
            <div class="row">
                <div class="col-lg-6 form-group">
                    <div class="row col-lg-12">
                        @Html.LabelFor(model => model.Loan.ClientId, new {@class = "col-lg-6 col-md-6 col-sm-6"})
                        @Html.TextBoxFor(model => model.Loan.ClientId, new {@class = "form-control col-lg-6 col-md-6 col-sm-6"})
                    </div>
                    <div class="row col-lg-12">
                        @Html.LabelFor(model => model.ScoreIdentifier, new {@class = "col-lg-6 col-md-6 col-sm-6"})
                        @Html.TextBoxFor(model => model.ScoreIdentifier, new {@class = "form-control col-lg-6 col-md-6 col-sm-6"})
                    </div>
                    <div class="row col-lg-12">
                        @Html.LabelFor(model => model.Score, new {@class = "col-lg-6 col-md-6 col-sm-6"})
                        @Html.TextBoxFor(model => model.Score, new {@class = "form-control col-lg-6 col-md-6 col-sm-6"})
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="row col-lg-12">
                        @Html.LabelFor(model => model.OperatorScore, new {@class = "col-lg-6 col-md-6 col-sm-6"})
                        @Html.TextBoxFor(model => model.OperatorScore, new {@class = "form-control col-lg-6 col-md-6 col-sm-6"})
                    </div>
                    <div class="row col-lg-12">
                        @Html.LabelFor(model => model.OperatorComment, new {@class = "col-lg-6 col-md-6 col-sm-6"})
                        @Html.TextAreaFor(model => model.OperatorComment, new {@class = "form-control col-lg-6 col-md-6 col-sm-6"})
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-3">
                    <button name="get-identifier" value="get-identifier" class="ui btn btn-primary col-lg-6 col-md-6 col-sm-6" onclick="GetIdentifier()">Get Identifier</button>
                </div>
                <div class="col-lg-3">
                    <button name="get-score" value="get-score" class="ui btn btn-primary col-lg-6 col-md-6 col-sm-6" onclick="GetScore()">Get Score</button>
                </div>
    
                <div class="col-lg-6 pull-right">
                    <button name="submit-score" value="submit-score" class="ui btn btn-primary col-lg-3 col-md-3 col-sm-3 pull-right" onclick="SubmitScore()">Submit Score</button>
                </div>
            </div>
        </form>
        
    </div>
    
    
    <script type="text/javascript">
    
        $('form#scoring-form').submit(function(e) {
            e.preventDefault();
            return;
        });
    
        function GetIdentifier() {
            $.ajax({
                type: "GET",
                url: '/Scoring/create-scoring',
                cache: false,
                data: $('#scoring-form').serialize(),
                // dataType: 'text/json',
                success: function (result) {
                   // console.log(result);
                   $('#scoring').html(result);
                }
            });
        }
    </script>
    
    请注意 #scoring id 是父 View 中的父 div。这只是部分观点。
    Controller :
    <ActionName("create-scoring")>
    Public Function CreateScoringFileForClient(oModel As LoanScoreModel) As ActionResult
        Dim oResult As ScoringResultModel = ScoringStrategy.CreateScoringFile(oModel)
        If oResult.IsSuccess Then
            If oResult.ResultFound Then
                oModel.ScoreIdentifier = oResult.ScoringModel.Identifier.ToString
                oModel.SuccessMessage = "Identifier Received"
    
                ScoringManager.SetLoanScoreIdentifier(oModel.LoanId, oModel.ScoreIdentifier)
               
                Return PartialView("_Scoring", oModel)
            Else
                Dim sErrorMessage = "No Reply From Scoring API"
                oModel.ErrorMessage = sErrorMessage
                Return PartialView("_Scoring", oModel)
            End If
        Else
            Dim sErrorMessage = "Couldn't call API"
            oModel.ErrorMessage = sErrorMessage
            Return PartialView("_Scoring", oModel)
        End If
    End Function
    
    我通过断点确保标识符不为空,并且使用 Return PartialView("_Scoring", oModel) 返回.
    当调用结束并返回结果时,我看到的唯一变化是显示的消息,但 TextBox 未修改:
    Result after Ajax Call
    我在调用 console.log(result) 后发现了问题。在成功的ajax函数中,我发现正在添加消息,但标识符没有,到目前为止我找不到解决方案。
    什么可能导致这个问题?以及如何解决?
    更新:
    我尝试将其添加到我的 View 中:
    <label class="text-danger">@Html.DisplayFor(model => model.ScoreIdentifier)</label>
    
    在其他两个标签下。
    当我在 ajax 中返回结果时,标签显示为正确的值,但文本框没有被更新。
    编辑:
    根据 Swati 的评论,我尝试将 @value 添加到 html 属性,如下所示:
    @Html.TextBoxFor(model => model.ScoreIdentifier, new { @class = "form-control col-lg-6 col-md-6 col-sm-6", @id = "identifier", @readonly = "readonly", @value = Model.ScoreIdentifier })
    
    这也没有更新 ajax 回复的值。但是,如果我在 View 中的其他位置使用 Model.ScoringIdentifier,它将显示实际值。
    我感觉它与@Html.TextBoxFor...
    为了回答 Chandan 的要求,这是我正在使用的模型:
    Public Class LoanScoreModel
        Public property Id As Integer
        Public property LoanId As Integer
        Public property ScoreIdentifier As String
        Public property Score As Double
        Public property IdentifierDateAcquired As DateTime
        Public property ScoreDateAcquired As DateTime
        Public property OperatorScore As Double
        Public property OperatorScoreDateAcquired As DateTime
        Public property OperatorComment As String
        Public property OperatorUserId As Guid
        Public Property Loan As Loan
        Public Property ErrorMessage As String = ""
        Public Property SuccessMessage As String = ""
    End Class
    
    Public Class ScoringResultModel
        Public Property IsSuccess As Boolean
        Public Property ResultFound As Boolean
        Public Property ErrorMessage As String
        Public Property ScoringModel As ScoringModel
    End Class
    
    Public Class ScoringModel
        Public Property Identifier As Guid
        Public Property Score As Double
    End Class
    

    最佳答案

    上次编辑问题时说I have a feeling it has something to do with @Html.TextBoxFor... ,我接着做了一些关于如何确保在 Ajax 调用后刷新文本框的搜索。
    我在 SO 上遇到了这两个问题:
    TextBoxFor is not refreshing after postback
    How to update the textbox value @Html.TextBoxFor(m => m.MvcGridModel.Rows[j].Id)
    引用第二个问题:

    That's because HTML helpers such as TextBoxFor first look in the ModelState when binding their values and only after that in the model. So if in your POST action you attempt to modify some value that was part of the initial POST request you will have to remove it from the ModelState as well if you want those changes to take effect in the view.


    最后,由于我使用的是 HTML 助手,我必须在返回模型之前清除我的模型状态,所以我的 Controller 被更改为:
    <ActionName("create-scoring")>
    Public Function CreateScoringFileForClient(oModel As LoanScoreModel) As ActionResult
        Dim oResult As ScoringResultModel = ScoringStrategy.CreateScoringFile(oModel)
        ModelState.Clear() 'https://stackoverflow.com/questions/11341545/how-to-update-the-textbox-value-html-textboxform-m-mvcgridmodel-rowsj-id
        oModel.ErrorMessage = ""
        oModel.SuccessMessage = ""
        If oResult.IsSuccess Then
            If oResult.ResultFound Then
                oModel.ScoreIdentifier = oResult.ScoringModel.Identifier.ToString
                oModel.SuccessMessage = Resources.Resources.IdentifierReceived
    
                ScoringManager.SetLoanScoreIdentifier(oModel.Loan.ID, oModel.ScoreIdentifier)
               
                Return PartialView("_Scoring", oModel)
            Else
                Dim sErrorMessage = Resources.Resources.ProblemAPINoResult
                oModel.ErrorMessage = sErrorMessage
                Return PartialView("_Scoring", oModel)
            End If
        Else
            Dim sErrorMessage = Resources.Resources.ProblemAPINoSuccess
            oModel.ErrorMessage = sErrorMessage
            Return PartialView("_Scoring", oModel)
        End If
    End Function
    
    这解决了这个问题,现在我的文本框在调用后用新数据刷新。

    关于jquery - Return PartialView ("view", model) 与 Ajax 正在更新标签,但不是文本框,即使对于相同的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65966463/

    相关文章:

    javascript - 使用jquery从ajax响应中提取

    c# - 如何在 .NET 中使用 Facebook signed_request?

    javascript - 将 div 添加到幻灯片轮播

    javascript - 将项目添加到 ObservableArray() 并等待添加?

    javascript - ajaxStart 和 ajaxStop 等同于 fetch API

    javascript - 2个函数,2个ajax请求,如何正确回调

    asp.net - 我可以修改 Global.asax 之外的 MVC 路由吗?

    asp.net-mvc - 在 asp.net-mvc 中,保存用户内容(图像、文件等)时是否有一个好的库或模式可以遵循

    javascript - 日历图标不显示

    javascript - 使用 jQuery 通过复选框禁用/启用按钮