@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方法。
答案 0 :(得分: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;