从阿贾克斯呈现局部视图发布数据传回控制器 [英] Posting data back to a controller from a Partial View rendered by Ajax

查看:105
本文介绍了从阿贾克斯呈现局部视图发布数据传回控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个Ajax调用返回的局部视图:

I am returning a Partial View from an Ajax call:

$(document).ready(function () {
$("#itemSubmitter").click(function (e) {
    $.ajax({
        url: '@Url.Action("GetShippingAddress", "Order")',
        type: "POST",
        cache: false,
        success: function (data) {

            $("#shoppingAddressWrapper").html(data);

        }
    });
});
});

这将返回如预期的视图。然而,偏有一些文本框与已填充的数据。该#shoppingAddressWrapper是一个表单标签内。

This returns the View as expected. However, the Partial has several Textboxes with data already populated. The #shoppingAddressWrapper is within a Form Tag.

当我提交表单,在文本框的部分的值不是Request.Params集合的一部分。

When I submit the Form, the values in the Textboxes on the partial are not part of the Request.Params collection.

这是正常的吗?你不能返回输入框为通过Ajax调用呈现局部视图部分,然后将数据上传到服务器,并检索值?

Is this normal? Can you not return input boxes as part of Partial View rendered via Ajax call and then post that data to the server and retrieve the values?

C

推荐答案

我已经找到了问题所在。
你需要清除的模型状态的文本框。

I have found what the problem is. You need to clear the model state for the text boxes.

if (Request.IsAjaxRequest())
    ModelState.Clear();

您可以清除你需要的值,或所有的值。 Ajax机制,否则将插入旧值回来了。

You can clear just the values you need, or all the values. The ajax mechanism will otherwise insert the old values back.

这篇关于从阿贾克斯呈现局部视图发布数据传回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆