发送局部视图模型和更新局部视图使用jQuery有问题 [英] Send partial view model and Update partial view with jQuery has issues

查看:214
本文介绍了发送局部视图模型和更新局部视图使用jQuery有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用内部的其他视图模型小模型的局部视图。所以我从包装形式抢夺模型数据,并使用 serializeArray()发送它的变化。然后从动作返回 PartialViewResult 最后通过返回的结果填补局部视图的div容器。这是我的代码:

I use partial view for small model inside other view model. so i send changes of it by grabbing model data from wrapper form and using serializeArray(). then return PartialViewResult from action and finally fill partial view div container by returned result. this is my code:

var modelStr = $("#[wrapperFormName]").serializeArray();
$.ajax({
type: "POST",
url: targetUrl,
cache: false,
data: modelStr,
success: function (sucResult) {
$('#pa_Cnt').html(sucResult);
},
fail: function (result) {
alert("Fail");
}
});

和渲染视图partialview就象这样:

and render partialview in view as this:

@using (Html.BeginForm("[ActionName]", "[CtrlName]", FormMethod.Post, new { id = "[wrapperFormName]", @enctype = "multipart/form-data" }))
{
    <div id="[partialcontainerName]">
        @Html.Partial("[partialViewName]", [partialViewModelName])
    </div>
}

一个问题是返回的结果不工作的任何更新局部视图后,的结合的部分中的元素,我知道的事件处理程序,必须声明在主视图中,而不是在部分和处理程序必须被委派的jQuery事件处理程序。
第二个问题是更新的结果具有新的值,但一些局部视图显示旧值的元素和我设置的ajax缓存为false与行动缓存[的OutputCache(持续时间= 0)]
我很困惑。谁能帮我。

One of issue is after update partial view with returned result don't work any of jQuery event handlers that bind to elements inside the partial and i know event handlers must be declare in main view and not in partial and handlers must be delegated. Second issue is updated result has new values but some of elements in partial view show old values and i set cache of ajax to false as cache of action with [OutputCache(Duration = 0)]. I'm confusing. Can anyone help me.

推荐答案

我找到了。感谢来自@大卫的第一个问题。我分开,与变量名指出职能下放事件的内部行为和部分与返回的结果更新HTML后附加事件委托给他们。这是一个简单的例子:

I found it. Thanks from @David for first issue. I separate internal actions of delegated events to functions that pointed with variable names and attach delegate events to them after update html of partial with returned result. this is a simple sample:

var sampleHandler = function() { //some code }

,然后在阿贾克斯成功:

and then in ajax success:

success: function (sucResult) {
         $('#pa_Cnt').html(sucResult);
         $("[parentClassNameSelector]").on("click", '[childSelector]', sampleHandler);
   }



关于MVC的第二个问题的ModelState是有罪的。 :)特别是在通过MVC佣工渲染的元素。于是我决定这样做的控制器动作

About second issue ModelState of MVC is Guilty. :) specially in elements that rendered by mvc helpers. So i decide to do this in controller action.

if (ModelState.IsValid) { ModelState.Clear(); }



我不知道这是好或者有其他问题。现在它是解决我的问题。我在等待新的意见和建议。非常感谢。

I don't know if this is good or has any other issue. For now it was solved my problem. I'm waiting for new comments and advises. Thanks so much.

这篇关于发送局部视图模型和更新局部视图使用jQuery有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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