C# 将多个参数从 url 传递到 html.actionlink
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13225786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing multiple parameters from url to html.actionlink
提问by melkisadek
I'm fairly new to MVC and am struggling with routing when I try to pass multiple parameters via the URL.
我对 MVC 还很陌生,当我尝试通过 URL 传递多个参数时,我正在为路由而苦苦挣扎。
From a page with the URL: /PersonCAFDetail/Index/3?memberid=4
从带有 URL 的页面: /PersonCAFDetail/Index/3?memberid=4
...I'm trying to get an Html.ActionLink set to point to the Create action such that the id=3 and the memberid=4.
...我正在尝试将 Html.ActionLink 设置为指向 Create 操作,以便 id=3 和 memberid=4。
Having read a number of similar posts it seems that the following should work:
阅读了许多类似的帖子后,似乎以下应该有效:
@Html.ActionLink("Create New", "Create", null, new { memberid = "memberid" })
However, this results in a URL being created as follows:
但是,这会导致按如下方式创建 URL:
<a href="/PersonCAFDetail/Create/3" memberid="memberid">Create New</a>
I have a route set up as:
我有一条路线设置为:
routes.MapRoute(
name: "PersonCAFDetail",
url: "PersonCAFDetail/Create/{id}/{memberid}",
defaults: new { controller = "PersonCAFDetail", action = "Create", id = "@\d+", memberid = @"\d+" }
);
The controller accepts two parameters as follows:
控制器接受如下两个参数:
public ActionResult Create(int id, int memberid)
{
int cafID = id;
int personID = memberid;
ViewBag.detailTypeID = new SelectList(db.tCAFDetailTypes, "detailTypeID", "detailType");
ViewBag.cafID = new SelectList(db.tFamilyCAFs, "cafID", "issues");
ViewBag.personID = new SelectList(db.tPersons, "personID", "forename");
return View();
}
Any help appreciated.
任何帮助表示赞赏。
-------edit for model----------
-------编辑模型-----------
namespace WhatWorks.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
public partial class tPersonCAFDetail
{
[Key, HiddenInput(DisplayValue=false)]
public int cafID { get; set; }
[Key, HiddenInput(DisplayValue = false)]
public int personID { get; set; }
[Key, HiddenInput(DisplayValue = false)]
public int detailTypeID { get; set; }
[Required, DataType(DataType.MultilineText)]
public string notes { get; set; }
public string FullName
{
get
{
return tPerson.forename + " " + tPerson.surname;
}
}
public virtual tCAFDetailType tCAFDetailType { get; set; }
public virtual tFamilyCAF tFamilyCAF { get; set; }
public virtual tPerson tPerson { get; set; }
}
}
采纳答案by Mate
Finally, you need pass two parameters to the view:
最后,您需要向视图传递两个参数:
Index action:
索引操作:
public ActionResult Index(int id, int memberid)
{
...
ViewBag.cafID = id;
ViewBag.personID = memberid;
return View();
}
Index.cshtml
索引.cshtml
@Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null)
And Check your route syntax... id = @"\d+"
并检查您的路由语法... id = @"\d+"
routes.MapRoute(
name: "PersonCAFDetail",
url: "PersonCAFDetail/Create/{id}/{memberid}",
defaults: new { controller = "PersonCAFDetail", action = "Create", id = @"\d+", memberid = @"\d+" }
);
回答by Simon Whitehead
Html.ActionLink(string, string, object, object)
..is what you're using. Those parameters are as follows:
..是你正在使用的。这些参数如下:
Html.ActionLink(<link text>, <action name>, <route values>, <html attributes>
You're placing your data into the attributes parameter, which will naturally make them attributes of your link (instead of them being route values).
您将数据放入属性参数中,这自然会使它们成为链接的属性(而不是路由值)。
Usage example:
用法示例:
@Html.ActionLink("Create new", "Create", new { id = Model.cafID, memberid = Model.personID }, null);
回答by user3081956
Cause that your Url.Action not working is that the & char in url is encoded, so you must use
导致您的 Url.Action 不起作用是 url 中的 & 字符已编码,因此您必须使用
@Html.Raw(Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null))
now, It's working ;)
现在,它正在工作;)
回答by Vijay Vj
You should create an ActionLink which has an overloaded method
您应该创建一个具有重载方法的 ActionLink
MvcHtmlString HtmlHelper.ActionLink(
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes
)
In your case :
在你的情况下:
@Html.ActionLink("Create New", "Create", PersonCAFDetail, new { id = "id", memberid = "memberid" })

