mvc 视图插入错误的“DOM”泰勒里克窗口

发布于 2025-01-02 12:51:30 字数 4174 浏览 1 评论 0原文

如果这成为一个很长的问题,请提前道歉...

背景信息

我有一个使用 Telerik 组件的 MVC 3 应用程序,并且这个特定问题是特定于(我认为)Window() 组件的。

从我的主视图 (Index.cshtml) 中,我执行一个 ajax 请求来返回一个部分视图,这就是我用来填充窗口内容的部分视图。这是正在执行请求的 jquery:

var url = '@Url.Action("GetAddPart", "Jobs")';            
var window = $("#Window").data("tWindow");
var data = $("#indexForm").serialize();
window.ajaxRequest(url, data);
window.center().open();

控制器操作是:

[HttpGet]
public ActionResult GetAddPart(MDTCompletedJobM model)
{
    // show the AddPart window

    //TryUpdateModel<MDTCompletedJobM>(model);
    model.ActionTakenList = ActionTakenList;
    model.ProblemTypes = ActualProblemList;
    var addPartM = new MDTAddPartM() { CompletedJobM = model };
    return PartialView(string.Concat(ViewDefaultUrl, "AddPart.cshtml"), addPartM);
}

这将打开我的窗口 hunky dory。

在我的部分视图中,我有一个包含两个或三个字段和一个“添加”、“取消”按钮的表单。为了简洁起见,我将只显示我认为部分视图的相关部分,但我可以生成整个视图(如果需要):

<div id="resultDiv">
    @using (Html.BeginForm("AddPart", "Jobs", FormMethod.Post, new { id = "addPartForm", name = "addPartForm" }))
    { 

             ** layout components removed from here **

        <input type="button" value="Add" class="t-button" id="btnAdd"/>
        <input type="button" value="Cancel" class="t-button" id="btnCancel"/>

        <p />
        <div id="progressdiv">
        </div>

    }    
</div>

是部分视图中的“顶级”标记,

用于处理取消按钮的是:

$("#btnCancel").click(function () {
    var window = $("#Window").data("tWindow");
    window.close();
});

在我的主视图中,我有一个提交按钮,完成后有效地重新显示主视图“禁用”或。显示此提交的操作。按钮返回主视图:

Controller's action snippet:
            if (ViewData["DoPayJobWarningStr"] == null)
                return RedirectToAction("GetAutoDispatchJob", new { autoDispatchJob = model.JobReferenceNumber});
            else
                return View(string.Concat(ViewDefaultUrl, "Index.cshtml"), tmpModel);

我的实际问题

对于我正在使用的特定示例,我期望 ViewData["DoPayJobWarningStr"] 不为空,那里将执行 return View(...)如果

我在不打开窗口的情况下执行此操作(针对提交按钮),我的视图将正确返回,并且页面会更新以显示警告消息,但是,如果我先打开窗口然后执行提交按钮,则视图不会。页面上未更新,但似乎已放入窗口的 html。即,如果我点击提交按钮(没有任何反应),然后单击打开 Telerik 窗口的按钮,我会短暂地看到提交操作返回的视图,然后再使用部分视图进行更新。应包含。我完全不明白返回的 View 是如何或为什么被放置在那里的?

我尝试过的事情:

  1. 注释掉 ajax 请求 (window.ajaxRequest(url, data);) 解决了问题(即使我显然有一个空白的部分视图可供查看)。
  2. 不使部分视图成为“表单”是行不通的
  3. 无论我如何“关闭”窗口,视图仍然放置在其中。例如,单击右上角的x
  4. 使用Firebug,单击提交按钮后的HTML 不会更新。
  5. 我也尝试过(得到相同的结果),而不是使用“window.ajaxRequest(url, data)”:

    <前><代码>$.ajax({ 类型:“获取”, 缓存:假, 网址: 网址, 数据:$("#indexForm").serialize(), 成功:函数(数据){ var window = $("#Window").data("tWindow"); 窗口.内容(数据); window.center().open(); $("#progress").html('') }, 错误:函数(e){ 警报(e.消息); } });

是否可以确定我做错了什么?是ajax请求吗?只是假设因为删除它可以解决问题,但可能还不止于此。

谢谢,当然如果您需要更多信息,请询问:)

谢谢

编辑

经过 3nigma 的建议,这就是我更新的内容(仍然没有运气)...

Telerik 窗口定义:

@{Html.Telerik().Window()
    .Name("Window")
    .Title("Add Part")
    .Draggable(true)
    .Modal(true)
    .Width(400)
    .Visible(false)
    .Height(270)
    .ClientEvents(e => e.OnOpen("onWindowOpen"))
    .Render();
}

jquery 函数是按钮的 OnClick 事件:

function AddBtnClicked() {
    $("#Window").data('tWindow').center().open();
}

onWindowOpen 事件:

function onWindowOpen(e) {
    //e.preventDefault();

    var data = @Html.Raw(Json.Encode(Model));
    var d2 = $.toJSON(data);

    $.ajax({
        type: 'GET',
        url: '@Url.Action("GetAddPart", "Jobs")',
        data: d2,
        contentType: 'application/json',
        success: function (data) {
            var window = $("#Window").data("tWindow");
            window.content(data);
            $("#progress").html('');
        },
        error: function (xhtr, e, e2) {
            alert(e +'\n' + xhtr.responseText);
        }
    });
};

Apologies in advance if this becomes a very long question...

Background Info

I have an MVC 3 application, using Telerik components and this particular issue is specific (I think) to the Window() component.

From my main view (Index.cshtml) I executing an ajax request to return a partial view which is what I am populating the contents of my window with. This is the jquery which is executing the request:

var url = '@Url.Action("GetAddPart", "Jobs")';            
var window = $("#Window").data("tWindow");
var data = $("#indexForm").serialize();
window.ajaxRequest(url, data);
window.center().open();

the controller action is:

[HttpGet]
public ActionResult GetAddPart(MDTCompletedJobM model)
{
    // show the AddPart window

    //TryUpdateModel<MDTCompletedJobM>(model);
    model.ActionTakenList = ActionTakenList;
    model.ProblemTypes = ActualProblemList;
    var addPartM = new MDTAddPartM() { CompletedJobM = model };
    return PartialView(string.Concat(ViewDefaultUrl, "AddPart.cshtml"), addPartM);
}

this opens my window hunky dory.

in my partial view i have a form with two or three fields and an "Add", "Cancel button. For the sake of brevity I'll just show what I think are the relevant parts of the partial view, but i can produce the entire view if need be:

<div id="resultDiv">
    @using (Html.BeginForm("AddPart", "Jobs", FormMethod.Post, new { id = "addPartForm", name = "addPartForm" }))
    { 

             ** layout components removed from here **

        <input type="button" value="Add" class="t-button" id="btnAdd"/>
        <input type="button" value="Cancel" class="t-button" id="btnCancel"/>

        <p />
        <div id="progressdiv">
        </div>

    }    
</div>

is the "top level" tag in the partial view.

my jquery to handle the Cancel button is:

$("#btnCancel").click(function () {
    var window = $("#Window").data("tWindow");
    window.close();
});

In my main view, I have a submit button, which when completed effectively reders the main view "disabled" or displays errors. The action for this submit button returns the main view:

Controller's action snippet:
            if (ViewData["DoPayJobWarningStr"] == null)
                return RedirectToAction("GetAutoDispatchJob", new { autoDispatchJob = model.JobReferenceNumber});
            else
                return View(string.Concat(ViewDefaultUrl, "Index.cshtml"), tmpModel);

My actual problem

For a specific example I am using, I am expecting ViewData["DoPayJobWarningStr"] NOT to be null, there the return View(...) will be executed.

if I execute this action (for the submit button) without opening the window, my view returns correctly and the page is updated to show the warning message. However, if I open the window first then execute the submit button, the View isn't updated on the page, but seems to be placed into the Window's html. ie, if I hit the submit button (nothing happens), then click on the button which opens the Telerik window, I briefly see the View returned by the submit Action being shown before it's updated with what the Partial View should contain. I don't understand at all how or why the returned View is being placed there?

Things I've tried:

  1. Commenting out the ajax request (window.ajaxRequest(url, data);) fixes the issue (even though I obviously have a blank partial view to look at).
  2. Not making the Partial View a "Form" doesnt' work
  3. No matter how I "close" the window, the view is still placed within there. eg clicking the x in the top right hand corner
  4. Using Firebug, the HTML after the submit button is clicked is not updated.
  5. Rather than using "window.ajaxRequest(url, data)", i've also tried (with the same result):

    $.ajax({
        type: "GET",
        cache: false,
        url: url,
        data: $("#indexForm").serialize(),
        success: function (data) {
            var window = $("#Window").data("tWindow");
            window.content(data);
            window.center().open();
            $("#progress").html('')
        },
        error: function (e) {
            alert(e.Message);
        }
    });
    

Is it all possible to determine what I am doing wrong? Is it the ajax request? Only assuming that because removing it fixes the issue, but there might be more to it than that.

Thanks and of course if you need more info, ask :)

Thanks

EDIT

After suggestions from 3nigma, this is what I've updated to (still no luck)...

Telerik window definition:

@{Html.Telerik().Window()
    .Name("Window")
    .Title("Add Part")
    .Draggable(true)
    .Modal(true)
    .Width(400)
    .Visible(false)
    .Height(270)
    .ClientEvents(e => e.OnOpen("onWindowOpen"))
    .Render();
}

jquery function which is an OnClick event for a button:

function AddBtnClicked() {
    $("#Window").data('tWindow').center().open();
}

the onWindowOpen event:

function onWindowOpen(e) {
    //e.preventDefault();

    var data = @Html.Raw(Json.Encode(Model));
    var d2 = $.toJSON(data);

    $.ajax({
        type: 'GET',
        url: '@Url.Action("GetAddPart", "Jobs")',
        data: d2,
        contentType: 'application/json',
        success: function (data) {
            var window = $("#Window").data("tWindow");
            window.content(data);
            $("#progress").html('');
        },
        error: function (xhtr, e, e2) {
            alert(e +'\n' + xhtr.responseText);
        }
    });
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2025-01-09 12:51:30

好的。

问题与部分视图中的这一行有关:

<script src="@Url.Content("~/Scripts/ValidationJScript.js")" type="text/javascript"></script>

当我把它拿出来后,一切都正常了。不知道为什么会导致这个问题 - 我认为它之前没有被加载过,但也许它已经加载了,这导致了问题。

尽管如此,还是感谢 3nigma 的想法!

OK.

Issue was related to this line in the Partial View:

<script src="@Url.Content("~/Scripts/ValidationJScript.js")" type="text/javascript"></script>

As soon as I took that out, it all works. Not sure why that was causing the issue - I don't think it had been previously been loaded but maybe it had which was causing the problem.

Thanks to 3nigma for your thoughts, nonetheless!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文