将多个参数从url传递到html.actionlink [英] Passing multiple parameters from url to html.actionlink
问题描述
当我尝试通过URL传递多个参数时,我对MVC还是很陌生,并且在路由方面苦苦挣扎.
I'm fairly new to MVC and am struggling with routing when I try to pass multiple parameters via the URL.
从具有以下网址的页面:/PersonCAFDetail/Index/3?memberid=4
From a page with the URL: /PersonCAFDetail/Index/3?memberid=4
...我正在尝试将Html.ActionLink设置为指向Create动作,以使id = 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.
阅读过许多类似的帖子,看来以下应该可行:
Having read a number of similar posts it seems that the following should work:
@Html.ActionLink("Create New", "Create", null, new { memberid = "memberid" })
但是,这将导致如下创建URL:
However, this results in a URL being created as follows:
<a href="/PersonCAFDetail/Create/3" memberid="memberid">Create New</a>
我的路线设置为:
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();
}
任何帮助表示赞赏.
-------编辑模型------------
-------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; }
}
}
推荐答案
最后,您需要向视图传递两个参数:
Finally, you need pass two parameters to the view:
索引操作:
public ActionResult Index(int id, int memberid)
{
...
ViewBag.cafID = id;
ViewBag.personID = memberid;
return View();
}
Index.cshtml
Index.cshtml
@Html.ActionLink("Create New", "Create", "PersonCAFDetail", new { id=ViewBag.cafID , memberid =ViewBag.personID}, null)
然后检查您的路线语法... id = @"\ d +"
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+" }
);
这篇关于将多个参数从url传递到html.actionlink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!