简体   繁体   English

将数据从JavaScript传递到MVC控制器

[英]Pass data from javascript to MVC controller

I have a tree hierarchy which I have built using Vis.js library. 我有一个使用Vis.js库构建的树层次结构。 My project is in asp.net MVC. 我的项目在asp.net MVC中。

Every node in the tree has an id. 树中的每个节点都有一个ID。 The requirement is that when I click on any of the node, the id should be passed to a controller and the view corresponding to the called controller action method should be rendered. 要求是,当我单击任何节点时,应将id传递给控制器​​,并应呈现与调用的控制器操作方法相对应的视图。

I have a view which displays the tree as follows: 我有一个显示树的视图,如下所示: 在此处输入图片说明

When I click any of the nodes in the tree, say 105, I want the node id to be passed to a contoller action method 'Tree1'. 当我单击树中的任何节点(例如105)时,我希望将节点ID传递到控制器操作方法“ Tree1”。 The method Tree1 will do some computation and render its own view. 方法Tree1将进行一些计算并呈现其自己的视图。

[HttpPost]
public ActionResult Tree1(string a)
{
    return View();
}

To pass the id from my tree view to the Tree1 controller action method, I am using $.ajax(). 要将ID从树形视图传递到Tree1控制器操作方法,我正在使用$ .ajax()。 This I found on various forums on the net. 这是我在网上的各种论坛上发现的。

network.on('selectNode',function(properties)
    {
        $.post({url: '@Url.Action("Tree1")',a:properties.nodes.toString()});
                                    @*$.ajax({
                                        url: '@Url.Action("Tree1")',
                                        type: 'POST',
                                        data: {a:properties.nodes.toString()},
                                        success: function(result) {
                                         //process the results from the controller
                                        }
                                    });*@
                                }
                ); 

Now, this does send the data to the method Tree1(I can see that when I debug), but the view of Tree1 is not rendered. 现在,这确实将数据发送到方法Tree1(调试时可以看到),但是未渲染Tree1的视图。 Instead, the previous view itself is rendered which shows the tree. 取而代之的是,呈现了先前的视图本身,该视图显示了树。

I want to pass the data from javascript to the controller action method such that no response is sent back to the calling javascript code. 我想将数据从javascript传递到控制器action方法,以便没有响应发送回调用javascript代码。 All the material on the net suggests solution which send back responses back to the calling javascript. 网上的所有材料都提出了解决方案,该解决方案将响应发送回给调用JavaScript。

Can you please help me with this? 你能帮我吗? Is there any basic concept that I am missing? 我缺少任何基本概念吗? How can I pass data from javascript to a controller method without the called method having to send any response back to the calling javascript? 我如何将数据从javascript传递到控制器方法,而被调用方法不必将任何响应发送回调用javascript?

Update 1 更新1

<script type="text/javascript">
    // create an array with nodes
    var nodes = new vis.DataSet([]);

        @foreach(DataRow @dr in Model.Tree.Tables[0].Rows)
        {
            @:nodes.add({id: @dr[0], label:@dr[0].ToString(), level:@dr[3]});
        }

    var edges = new vis.DataSet([]);
        @foreach(DataRow @dr in Model.Tree.Tables[0].Rows)
        {
            if(@dr[2].ToString()!="")
            {
                @:edges.add({from:@dr[2], to:@dr[0]});
            }
        }

    // create a network
    var container = document.getElementById('mynetwork');

    // provide the data in the vis format
    var data = {
        nodes: nodes,
        edges: edges
    };
    var options = {nodes:{shape:'ellipse'},edges:{arrows: 'to'},physics:true};

    // initialize your network!
    var network = new vis.Network(container, data, options);

    network.on('selectNode',function(properties)
    {

                                    $.ajax({
                                        url: '@Url.Action("Tree1")',
                                        type: 'POST',
                                        data: {a:properties.nodes.toString()},
                                        success: function(result) {
                                         //process the results from the controller
                                        }
                                    });
                                }
                );


</script>

Within the HTML of the original view you should have a container div, something like: 在原始视图的HTML中,您应该有一个容器div,类似于:

<div id="container">
... Original tree is here
<div id="container">

Then on the success response you have to put it inside this container: 然后,在成功响应上,您必须将其放入此容器中:

$.ajax({
    url: '@Url.Action("Tree1")',
    type: 'POST',
    data: {a:properties.nodes.toString()},
    success: function(result) {
        $("#container").html(result);
    }
});

EDIT: I forgot that you must do this returning partial view 编辑:我忘记了您必须执行此操作返回部分视图

[HttpPost]
public ActionResult Tree1(string a)
{
    return PartialView();
}

Since you wanted to maybe just navigate to the view, you can just use this: 由于您可能只想导航至视图,因此可以使用以下代码:

network.on('selectNode',function(properties)
{
    var url = '@Url.Action("Tree1")' + '?a=' +properties.nodes.toString();
    document.location = url;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM