gpt4 book ai didi

c# - 将多个参数从 url 传递到 html.actionlink

转载 作者:太空狗 更新时间:2023-10-29 21:09:43 25 4
gpt4 key购买 nike

我是 MVC 的新手,当我尝试通过 URL 传递多个参数时,我遇到了路由问题。

来自具有 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>

我有一条路线设置为:

    routes.MapRoute(
name: "PersonCAFDetail",
url: "PersonCAFDetail/Create/{id}/{memberid}",
defaults: new { controller = "PersonCAFDetail", action = "Create", id = "@\d+", memberid = @"\d+" }
);

Controller 接受如下两个参数:

 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; }
}
}

最佳答案

最后,您需要向 View 传递两个参数:

索引操作:

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+" }
);

关于c# - 将多个参数从 url 传递到 html.actionlink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13225786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com