从路由路径中获取actionlink参数

发布于 2024-08-24 02:10:53 字数 390 浏览 10 评论 0原文

我正在尝试添加一个网络表单,允许用户添加具有特定外键的数据库条目。

我正在创建这样的链接

<%= Html.ActionLink("Edit", "EditSub", new { id = id }) %>

,生成的 URL 为 http://localhost:3015/TumourGroup/CreateSub /2 (其中 2 是我之前传递给操作链接的 id)。问题是,如何从 URL 中检索 id 的值?我使用它来获取“主”表,以便我可以创建一个新的表条目,该条目具有指向“主”表的外键。

I'm trying to add a webform that allows the user to add a database entry with a specific foreign key.

I'm creating the link like this

<%= Html.ActionLink("Edit", "EditSub", new { id = id }) %>

and the resulting URL is http://localhost:3015/TumourGroup/CreateSub/2 (where 2 is the id I passed to the actionlink earlier). The question is, how do I retrieve the value of id from the URL? I'm using it to grab the "main" table so that I can create a new table entry that has a foreign key pointing to the "main" table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你曾走过我的故事 2024-08-31 02:10:54

让你的控制器函数 CreateSub 接受 int id

public ActionResult CreateSub (int id)

如果你想使用这个 id 进入表单,然后发布一组不同的数据,你需要两个函数,区别在于

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CreateSub (int id)

get 用于导航到你的输入表单, post 在表单发布时被调用。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)

下面是对评论中澄清的回应:

好吧,如果您放弃强类型视图,您可以这样做

ViewData["id"] = id;
ViewData["subGroupToCreate"] = ...

或者您可以在客户端的第二种形式中使用 JavascriptJquery

Have your controller function CreateSub take in int id

public ActionResult CreateSub (int id)

If you want to go to the form with this id, then post with a different set of data, you'd need two functions, differentiated by

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CreateSub (int id)

The get is for navigating to your entry form, the post is called when the form posts.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)

BELOW IS RESPONDING TO CLARIFICATION IN COMMENTS:

Well, if you forego the strongly typed view, you can just do

ViewData["id"] = id;
ViewData["subGroupToCreate"] = ...

Alternatively you can do it in the second form on the client side with Javascript or Jquery

君勿笑 2024-08-31 02:10:53

假设 TumourGroup 是控制器的名称,并且您有一个如下所示的路由:

routes.MapRoute(
    "Default",
    "{controller}, {action}, {id}",
    new { controller="Home", action="Index", id="" }
)

然后在您的 TumourGroup 控制器中,您只需要一个如下所示的控制器方法this:

public ActionResult CreateSub (int id)
{
    // blah
}  

参数 id 将包含 Url 中的您的 id。

编辑:要在提交表单时包含 id:

public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)
{
    // blah
}  
  1. 将 id 作为属性添加到您的 TumorGroupSubcategory 类中。

  2. 在您提交的表单视图中,包含一个与 TumorGroupSubcategory 类中的 id 命名相同的隐藏字段,并使用您的 id 字段填充它。

当用户提交表单时,模型绑定器将拾取该字段,并自动将其放入 tumourSubgroupToCreate 的 id 属性中。

Assuming that TumourGroup is the name of a controller, and you have a route that looks something like this:

routes.MapRoute(
    "Default",
    "{controller}, {action}, {id}",
    new { controller="Home", action="Index", id="" }
)

Then in your TumourGroup controller, you just need a controller method that looks like this:

public ActionResult CreateSub (int id)
{
    // blah
}  

The parameter id will contain your id from the Url.

EDIT: To include the id when you are submitting a form:

public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)
{
    // blah
}  
  1. Add the id as a property to your TumorGroupSubcategory class.

  2. In the form view you are submitting, include a hidden field that is named the same as the id in your TumorGroupSubcategory class, and populate it with your id field.

When your user submits the form, the Model Binder will pick up the field, and put it into the id property of tumourSubgroupToCreate automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文