是否可以通过@Html.ActionLink传递参数?



我需要使用相同的视图,但根据用户选择的按钮显示在表中,具有不同的数据。我想通过html.actionlink传递一个参数来做到这一点。

这是我目前在视图中的内容

<li id="liOpenJobs">@Html.ActionLink("All Jobs", "AllJobs", "AdminJobs", new { filter = "All" })</li>

控制器

[HttpGet]
public ActionResult AllJobs(string filter)
{
if (PCSSession.Current.IsAuthenticated)
{
var jobs = new JobRepository();
var model = new JobsHistoryViewModel();
if(filter == "All")
{
model.jobHistory = jobs.GetAllJobs("alljobs");
}

return View(model);
}
return RedirectToAction("AdminLogin", "AdminLogin");
}

使用此ActionLink帮助程序方法的重载。

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
RouteValueDictionary routeValues,
IDictionary<string, Object> htmlAttributes
)

第四个参数是路由值对象,它将用于为锚标记的href属性值生成查询字符串项。第五个参数是 htmlAttributes。如果您没有,只需通过null

@Html.ActionLink("All Jobs", "AllJobs", "AdminJobs", new { filter = "All" }, null)
@Html.ActionLink("Some Jobs", "AllJobs", "AdminJobs", new { filter = "some"}, null)

这将为具有如下值href锚标记生成 HTML 标记

/AdminJobs/AllJobs?filter=All/AdminJobs/AllJobs?filter=some

最新更新