6

我对 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; }
    }
}
4

4 回答 4

13

最后,您需要将两个参数传递给视图:

索引操作:

public ActionResult Index(int id, int memberid)
{
    ...
    ViewBag.cafID = id;
    ViewBag.personID = memberid;
    return View();
}

索引.cshtml

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

并检查您的路线语法... id = @"\d+"

 routes.MapRoute(
    name: "PersonCAFDetail",
    url: "PersonCAFDetail/Create/{id}/{memberid}",
    defaults: new { controller = "PersonCAFDetail", action = "Create", id = @"\d+", memberid = @"\d+" }                        
    );
于 2012-11-05T04:15:57.780 回答
0
Html.ActionLink(string, string, object, object)

..是你正在使用的。这些参数如下:

Html.ActionLink(<link text>, <action name>, <route values>, <html attributes>

您将数据放入 attributes 参数中,这自然会使它们成为链接的属性(而不是成为路由值)。

使用示例:

@Html.ActionLink("Create new", "Create", new { id = Model.cafID, memberid = Model.personID }, null);
于 2012-11-05T04:17:10.223 回答
0

导致您的 Url.Action 不起作用的原因是 url 中的 & char 已编码,因此您必须使用

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

现在,它正在工作;)

于 2013-12-09T07:52:54.943 回答
0

您应该创建一个具有重载方法的 ActionLink

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes
)

在你的情况下:

@Html.ActionLink("Create New", "Create", PersonCAFDetail, new { id = "id", memberid = "memberid" })
于 2019-05-27T06:32:19.557 回答