Passing multiple parameters from url to html.actionlink

走远了吗. 提交于 2019-12-05 06:43:27

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

@Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null)

And Check your route syntax... id = @"\d+"

 routes.MapRoute(
    name: "PersonCAFDetail",
    url: "PersonCAFDetail/Create/{id}/{memberid}",
    defaults: new { controller = "PersonCAFDetail", action = "Create", id = @"\d+", memberid = @"\d+" }                        
    );
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);
user3081956

Cause that your Url.Action not working is that the & char in url is encoded, so you must use

@Html.Raw(Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null))

now, It's working ;)

You should create an ActionLink which has an overloaded method

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" })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!