3

我有一个使用 jquery 发布到服务器的部分视图。该帖子正在运行。但是,返回时,我返回的是带有空白模型(新模型)的 partialviewresult,但返回的 HTML 仍然包含以前发布的数据。关于清除退货数据的任何想法?

$("#btnSend").click(function (e) {
    e.preventDefault();

    if($('#frmCompose').valid()) {
        $.ajax({
            type: "POST",
            url: '@Url.Action("PartialCompose", "Message")',
        dataType: "html",
        data: $('#frmCompose').serialize(),
        success: function (result) {
            $("#divTab2").html(result);
        },
        error: function (xhr, s, e) {
            alert('Error');
            alert(xhr.responseText);
        }
        });
    }        
});

这是行动:

    [SessionExpireFilterAttribute]
    [HttpPost] // POST: /message/partialcompose
    public PartialViewResult PartialCompose(_MessageExt model)
    {
        _MessageExtDAL __DAL = new _MessageExtDAL(base.LoginTimeZoneMin);
        try
        {
            model.MessageId = 0;
            model.AccountId = base.LoginUser.AccountId;
            model.EditBy = base.LoginUser.UserId;

            __DAL.Send(model, Config.SQLConn);
        }
        catch (Exception ex)
        {
            base.Prompt = ex.Message;
        }
        finally
        {
            __DAL = null;
        }
        return PartialView(new _MessageExt());
    }
4

1 回答 1

3

这是因为您直接从 POST 操作返回相同的视图,并且表单值仍然存在于ModelState.

这是设计使然,主要是为了与原始表单数据一起显示验证错误。in 的值比传递给视图的模型对象中的值ModelState具有更高的优先级ModelState,因此如果两者都存在,则将使用 in 的值。

以下应该看到清除的字段:

ModelState.Clear();
return PartialView(new _MessageExt());
于 2014-07-17T15:13:45.123 回答