[英]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.