简体   繁体   English

ASP.NET MVC如何将@ Ajax.ActionLink中的参数发送到控制器中的方法

[英]ASP.NET MVC how to send param from @Ajax.ActionLink to method in controller

@Html.ActionLink((string)Model[i]["name"], "Info", "Home", new { uid = Model[i]["uid"].ToString() }, null)  

[HttpGet]
            public ActionResult Info(string uid)
            {
                TempData["uid"] = uid;
                SearchModel model = new SearchModel();
                List<DataTable> tables = model.GetUserInfo(uid);
                TempData["tables"] = tables;

                return View(model);
            }

In the controller, there is a method that returns JsonResult. 在控制器中,有一个返回JsonResult的方法。 This method is used to obtain data, on the basis of which the js code will be executed. 此方法用于获取数据,在此基础上将执行js代码。

public JsonResult GetCountryRisk(SearchModel model)
    {
        string uid = TempData["uid"].ToString();
        IEnumerable<CountryRisk> list = new List<CountryRisk>();
        list = model.GetCountryRisk(uid);
        TempData["uid"] = uid;
        return Json(list, JsonRequestBehavior.AllowGet);
    }

The view has elements that simulate tabs and when clicked on the selected item, Ajax is called, which receives the data and passes it to the script. 该视图具有模拟选项卡的元素,当单击所选项目时,将调用Ajax,Ajax接收数据并将其传递给脚本。

    <div id="tabHeadDiv">
        <input type="button" value="Summary" class="tabControl" id="1" onclick="ShowOrHideTabs(this.id)" />
        @Ajax.ActionLink("Relations", "Info", null, new AjaxOptions { Url = Url.Action("GetDataForGraph"), OnSuccess = "DrawGraph" }, new { @id = "2", @class = "tabControl" })
        @Ajax.ActionLink("Map", "Info", null, new AjaxOptions { Url = Url.Action("GetCountryRisk"), OnSuccess = "drawRegionsMap" }, new { @id = "3", @class = "tabControl" })
    </div>

Problem is that if a user opens several links in a new tab and wants to go to the tab, then on all tabs the result will be displayed, which was executed last. 问题是,如果用户在一个新选项卡中打开多个链接并想要转到该选项卡,那么将在所有选项卡上显示结果,该结果最后执行。 So I want to understand if it's possible to send parameter to the GetCountryRisk method without using TempData ["uid"] . 因此,我想了解是否可以在不使用TempData ["uid"]的情况下将参数发送到GetCountryRisk方法。

First, stop using a ViewBag or a TempData for uid . 首先,停止对uid使用ViewBag或TempData。 This is clearly a part of your state, and while these key-value stores have their uses, they are no good for your case. 这显然是您状态的一部分,尽管这些键值存储有其用途,但它们对您的情况不利。

Make uid a part of SearchModel , as it is obviously important for you as you handle search requests, and in Info action do this: 使uid成为SearchModel的一部分,因为在处理搜索请求时,这对您显然很重要,在Info操作中,请执行以下操作:

SearchModel model = new SearchModel();
model.UID = uid;

Now on the page I don't really understand why the Ajax.ActionLink mentions one controller/action pair and then uses another as the URL. 现在,在页面上,我真的不明白为什么Ajax.ActionLink会提到一个控制器/动作对,然后使用另一个作为URL。 Here is how I would imagine it: 这是我的想象:

@Ajax.ActionLink(
    "Your controller name",
    "GetCountryRisk",
    new {uid = Model.UID},
    new AjaxOptions {OnSuccess = "drawRegionsMap" },
    new { @id = "3", @class = "tabControl" })

Finally in the GetCountryRisk action just use uid: 最后,在GetCountryRisk操作中,只需使用uid:

public JsonResult GetCountryRisk(SearchModel model)
{
    string uid = model.UID;

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

相关问题 如何通过ActionLink将项目发送到ASP.NET MVC控制器? - How to send item through ActionLink to ASP.NET MVC controller? 如何在通过Ajax.ActionLink调用的动态加载的ASP.NET MVC局部视图上初始化jsTree - How can I initialize jsTree on a dynamically loaded ASP.NET MVC Partial View that is called via an Ajax.ActionLink Asp.net MVC 2:Ajax.ActionLink是否可以传递参数而不将其输入url? - Asp.net MVC 2: Can Ajax.ActionLink pass a parameter without it going into the url? ASP .NET MVC 5 - 如何在 Ajax.ActionLink 中添加表格 model? - ASP .NET MVC 5 - How to add form model inside Ajax.ActionLink? 使用ajax将文本框中的数据发送到asp.net mvc 5控制器 - Send data from textbox with ajax to asp.net mvc 5 controller 如何将数组发送到asp.net mvc中的另一个控制器方法? - How to send array to another controller method in asp.net mvc? 将布尔从视图/ ActionLink返回到方法-Asp.Net MVC - Return bool from view/ActionLink to method - Asp.Net MVC 如何在ASP.Net MVC中调用控制器方法并从Ajax调用传递对象 - How to call controller method and pass the object from ajax call in ASP.Net MVC 如何在提交前使用 Ajax 从 asp.net mvc 登录表单调用控制器方法 - How to call controller method from asp.net mvc Login Form using Ajax before submit 如何在MVC ASP.NET中使用AJAX将模型从View发送到bool方法 - How to send model from View to bool method with AJAX in MVC ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM