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

Return PartialView("view", model) with Ajax is updating the labels but not the text boxes, even for same property

我正在使用 MVC 5,VB.NET,jQuery。

在我的一个页面上,我正在使用 ajax 调用调用一个操作,并尝试根据结果重新加载 html。

我正在修改两个属性:

  1. 留言
  2. 标识符

正在用预期的结果替换消息,但是,标识符不是。

代码如下:

查看:

@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 是父视图中的父 div。这只是部分视图。

控制器:

<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:

我在successajax函数里面调用了console.log(result)之后发现了问题,发现是添加了message,但是没有添加Identifier,我做不到到目前为止找到解决方案。

是什么导致了这个问题?以及如何修复它?

更新:

我尝试将此添加到我的视图中:

<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 回复的值。但是,如果我在视图的其他地方使用 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 助手,我必须在返回模型之前清除我的模型状态,所以我的控制器更改为:

<ActionName("create-scoring")>
Public Function CreateScoringFileForClient(oModel As LoanScoreModel) As ActionResult
    Dim oResult As ScoringResultModel = ScoringStrategy.CreateScoringFile(oModel)
    ModelState.Clear() '
    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

这解决了问题,现在我的文本框在调用后用新数据刷新。