剃刀视图jQuery ajax调用 - 模型未完全填充

时间:2014-02-07 17:50:10

标签: jquery ajax asp.net-mvc razor

我有与相关jQuery ajax点击调用的按钮。传入的模型有一些填充的字段,但不是全部。

有问题的按钮作为部分内容包含在页面中。部分页面如下所示:

<div class="modal fade" id="adjustScoreModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Adjust Scoring Values</h4>
            </div>
            <div class="modal-body">
                <table>
                    @foreach (VIMS.Web.Models.Measure m in Model.list)
                    {
                        <tr>
                            <td>
                                @Html.HiddenFor(i => m.id)
                                @Html.LabelFor(i => m.measure) : 
                                @Html.DisplayFor(i => m.measure)
                            </td>
                            <td colspan="2">
                                @Html.LabelFor(i => m.lastModified) :
                                @Html.DisplayFor(i => m.lastModified)
                            </td>
                        </tr>
                        <tr class="range">
                            <td>
                                @Html.LabelFor(i => m.weight)
                                <br />
                                @Html.TextBoxFor(i => m.weight)
                            </td>
                            <td>
                                @Html.LabelFor(i => m.zeroValue)
                                <br />
                                @Html.TextBoxFor(i => m.zeroValue)
                            </td>
                            <td>
                                @Html.LabelFor(i => m.hundredValue)
                                <br />
                                @Html.TextBoxFor(i => m.hundredValue)
                            </td>
                        </tr>
                        <tr class="range">
                            <td>
                                @Html.LabelFor(i => m.increment)
                                <br />
                                @Html.TextBoxFor(i => m.increment)
                            </td>
                            <td>
                                @Html.RadioButtonFor(i => m.rangeBased, "true", new { name=m.id.ToString()+ "Range"}) Range based incrememnt
                            </td>
                            <td>
                                @Html.RadioButtonFor(i => m.rangeBased, "false", new { name = m.id.ToString() + "Range" }) Always incremented
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <hr />
                            </td>
                        </tr>
                    }
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" id="updateMeasures" class="btn btn-default" data-dismiss="modal">SAVE</button>
                <button type="button" id="checkMeasures" class="btn btn-default" data-dismiss="modal">Check Values</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

jQuery块看起来像这样:

//modal button click events to update/check measure values
$('#updateMeasures').click(function () {
    $.ajax({
        url: '@Url.Action("UpdateScoring", "SendingReport", Model)',
        type: 'POST'
        })
    })
$('#checkMeasures').click(function () {
    $.ajax({
        url: '@Url.Action("CheckScoring", "SendingReport", Model)',
        type: 'POST'
    })
})

此处传递的模型已填充了一些属性,但未填充其他属性。我怀疑它与构建url时有关,因为模型似乎是从上次发布页面时开始的。如何在呈现页面时确保刷新ajax函数中引用的模型?

1 个答案:

答案 0 :(得分:1)

感谢AntP的评论,因为这是我需要知道要寻找什么的差异。最终工作的神奇之处在于改变了jQuery,因此它不完全依赖于URL字符串,而是以更传统的jQuery意义实际传递数据:

//modal button click events to update/check measure values
$('#updateMeasures').click(function () {
    $.ajax({
        url: '@Url.Action("UpdateScoring", "SendingReport")',
        data: $('form').serialize(),
        type: 'POST'
        })
    })
$('#checkMeasures').click(function () {
    $.ajax({
        url: '@Url.Action("CheckScoring", "SendingReport")',
        data: $('form').serialize(),
        type: 'POST'
    })
})