ASP.NET MVC如何从@ Ajax.ActionLink向控制器中的方法发送param

问题描述 投票:0回答:1
@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);
            }

在控制器中,有一个返回JsonResult的方法。该方法用于获取数据,在此基础上将执行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);
    }

视图具有模拟选项卡的元素,当单击选定项时,将调用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>

问题是,如果用户在新选项卡中打开多个链接并想要转到选项卡,则在所有选项卡上将显示最后执行的结果。所以我想了解是否可以在不使用TempData ["uid"]的情况下将参数发送到GetCountryRisk方法。

c# asp.net-mvc
1个回答
1
投票

首先,停止对uid使用ViewBag或TempData。这显然是您所在州的一部分,虽然这些键值商店有其用途,但它们对您的情况并不好。

uid成为SearchModel的一部分,因为当你处理搜索请求时它显然很重要,并且在Info动作中这样做:

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

现在在页面上,我真的不明白为什么Ajax.ActionLink提到一个控制器/动作对,然后使用另一个作为URL。以下是我的想象:

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

最后在GetCountryRisk操作中使用uid:

public JsonResult GetCountryRisk(SearchModel model)
{
    string uid = model.UID;
© www.soinside.com 2019 - 2024. All rights reserved.