我对 MVC 相当陌生,当我尝试通过 URL 传递多个参数时,我正在努力解决路由问题。
从带有 URL 的页面:/PersonCAFDetail/Index/3?memberid=4
...我正在尝试将 Html.ActionLink 设置为指向 Create 操作,以便 id=3 和 memberid=4。
阅读了许多类似的帖子后,似乎以下内容应该有效:
@Html.ActionLink("Create New", "Create", null, new { memberid = "memberid" })
但是,这会导致创建如下 URL:
<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+" }
);
控制器接受如下两个参数:
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();
}
任何帮助表示赞赏。
--------模型编辑----------
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; }
}
}