MVC kendo window - Get Data from javascript function

天大地大妈咪最大 提交于 2019-12-10 11:07:29

问题


I have this kendo window in my app

Html.Kendo().Window()
    .Name("copyStructure")
    .Title("Copy Structure")
    .Content("Loading...")
    .LoadContentFrom("CopyStructure", "NewXmlLayout") // <-- here*
    .Draggable(false)
    .Visible(false)
    .Modal(true)
    .Actions(s => s.Custom(""))
    .Events(e => e.Open("openWindow").Close("closeWindow"))

And I am trying to pass data to the acrion in the LoadContentFrom() wich is returned by a javascript function, but I don't know how to do it. I can pass data like this:

.LoadContentFrom("CopyStructure", "NewXmlLayout", new { type= "INPUT" })

But is not what I'm looking for.

The JS function:

function getInfo() {
        return { type: "INPUT" };
    };

My controller:

 public ActionResult CopyStructure(string type)
    {
        return PartialView();
    }

回答1:


If you really need to access your data via the JavaScript getInfo() function, then the way to do this is to define the Window as you are doing but don't set the content until you open the window. When opening the Window, use jQuery.ajax() to call CopyResult, passing the results of getInfo() into the data parameter.

In your Razor, take out LoadContentFrom add an event handler for the Open event:

@(Html.Kendo().Window()
    .Name("copyStructure")
    // Omitted for brevity
    ...
    .Events(e => e.Open("copyStructure_Open"))
)

And in your handler in JavaScript, call $.ajax and in the success callback, call the content method on the Window object passing the returned data as the parameter:

function copyStructure_Open(e) {
    $.ajax({
        url: '@Url.Action("CopyStructure", "NewXmlLayout")',
        type: 'POST',
        data: getInfo(),
        success: function(data) {
            e.sender.content(data);
        }
    });
}

Beware to only send what's required for the window content, and not a full page (DOCTYPE, html, head, body) - see this documentation from Telerik: http://docs.telerik.com/kendo-ui/getting-started/web/window/overview#loading-window-content-via-ajax



来源:https://stackoverflow.com/questions/24294449/mvc-kendo-window-get-data-from-javascript-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!