如何调用控制器方法并从ASP.Net MVC中的ajax调用传递对象

时间:2017-01-17 19:22:39

标签: c# asp.net-mvc asp.net-mvc-5

我正处于学习过程中并致力于ASP.net MVC 5项目。所以,我有一个模型视图,其中包含其他模型视图。

父模型视图

public class FullConfigMV
{
 some properties here
 public ElementStyleMV Page { get; set; }
}

以下是我想要实现的目标。

  1. 拨打ajax电话

  2. 一旦ajax调用点击控制器功能设置了一些值

  3. 现在从那里我需要将该对象传递给另一个控制器动作,该动作将返回它的视图。

  4. 第1步。

    <script>
        $(document).ready(function()
        {
            $("#tan").change(function()
            {
                alert(this.value);
                $.ajax({
                    type: 'POST',
                    url: '/Settings/GetAvailableLocationsFor',
                    data: { accountId: 28462, groupId: 35},
                    success: function (data) {
                        //Whatever
                    },
                    error: function () {
                        DisplayError('Failed to load the data.');
                    }
                });
    
    
            });
        });
    </script>
    

    在第1步之后,我的断点点击

    public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
            {
                FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
                Utils.SiteCss = configData.CssStyles();
               // NOW AT THIS PLACE I WANT TO CALL ANOTHER CONTROLLER FUNCTION
               // AND PASS THE `configData Obj`
                return View();
            }
    

    我知道我们有像

    这样的东西
    return RedirectToAction("Index","Home"); 
    

    但是如何通过config Obj

    我要调用的控制器功能在  Home Controller,操作名称为Index

    public ActionResult Index(FullConfigMV data)
            {
                return View();
            }
    

    如果要求看起来很奇怪,那么请耐心地对此表示敬意。

    修改

    解决方案建议“使用TempData”之后但问题是我的控制器中有两个索引操作。我希望SECOND索引操作受到攻击。但第一个受到了打击。

    首先

    public ActionResult Index()
    {
    }
    

    第二

    [HttpPost]
    public ActionResult Index(FullConfigMV data)
    {
    } 
    

    使用的代码是

        public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
        {
            FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
            SimUtils.SiteCss = configData.CssStyles();
            TempData["config"] = configData;
            return RedirectToAction("Index", "Home");
        }
    

3 个答案:

答案 0 :(得分:1)

您可以使用TempData实现此目的。来自https://msdn.microsoft.com/en-us/library/system.web.mvc.tempdatadictionary(v=vs.118).aspx

“TempDataDictionary对象的一个​​典型用途是在重定向到另一个操作方法时从操作方法传递数据。”

那里有很多文档,但只需使用

@javax.xml.bind.annotation.XmlSchema(namespace = "http://webService.othercompany.com/")
package com.mycompany.services.external.client;

在您的第一个动作结果中,并在您的主页&gt;中检索它索引

TempData["config"] = configData;

答案 1 :(得分:0)

我不是百分之百,但你可能能够将其作为路线传递..如果没有,则使用.as-console-wrapper { max-height: 100% !important; top: 0; }

示例:

TempData

或者,如果以上不起作用:

public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
{
    FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
    Utils.SiteCss = configData.CssStyles();
    return RedirectToAction("Index","Home", new { data = configData});
}

如果有帮助,请告诉我

答案 2 :(得分:0)

对于你的问题 解决方案建议“使用TempData”之后但问题是我的控制器中有两个索引操作。我希望SECOND Index Action受到重创。但是第一个被击中你可以做的是你可以在TempData中设置的一些数据,你可以从你的第一个索引方法调用return Index(data),你可以得到数据通过Tempdata或其他变量。这将通过第一个索引方法调用第二个索引方法。