我對MVC相當陌生,當我嘗試通過URL傳遞多個參數時,我正在努力應付路由。將多個參數從url傳遞給html.actionlink
從與URL的網頁:/PersonCAFDetail/Index/3?memberid=4
...我試圖讓一個Html.ActionLink設置爲指向創建行動使得ID = 3和MEMBERID = 4。
看了一些類似的帖子,似乎下面應該工作:
@Html.ActionLink("Create New", "Create", null, new { memberid = "memberid" })
然而,這導致URL被創建如下:
<a href="/PersonCAFDetail/Create/3" memberid="memberid">Create New</a>
我有一個路由集合up:
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; }
}
}
我想傳遞參數,所以值會有所不同。我需要知道如何將參數值傳遞給「id」和「memberid」。路線語法是否不具有用數字通配符設置的ID? – melkisadek
好的,請展示如何將模型或ViewBag定義到視圖 – Mate
如果使用強類型視圖,請檢查.cshtml的第一行... @model「YourViewModel」。或者,如果您在ViewBag中發送類似ViewBag.MemberID的值? – Mate