从路由路径中获取actionlink参数
我正在尝试添加一个网络表单,允许用户添加具有特定外键的数据库条目。
我正在创建这样的链接
<%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让你的控制器函数 CreateSub 接受 int id
如果你想使用这个 id 进入表单,然后发布一组不同的数据,你需要两个函数,区别在于
get 用于导航到你的输入表单, post 在表单发布时被调用。
下面是对评论中澄清的回应:
好吧,如果您放弃强类型视图,您可以这样做
或者您可以在客户端的第二种形式中使用 Javascript 或 Jquery
Have your controller function CreateSub take in 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
The get is for navigating to your entry form, the post is called when the form posts.
BELOW IS RESPONDING TO CLARIFICATION IN COMMENTS:
Well, if you forego the strongly typed view, you can just do
Alternatively you can do it in the second form on the client side with Javascript or Jquery
假设
TumourGroup
是控制器的名称,并且您有一个如下所示的路由:然后在您的
TumourGroup
控制器中,您只需要一个如下所示的控制器方法this:参数 id 将包含 Url 中的您的 id。
编辑:要在提交表单时包含 id:
将 id 作为属性添加到您的
TumorGroupSubcategory
类中。在您提交的表单视图中,包含一个与
TumorGroupSubcategory
类中的 id 命名相同的隐藏字段,并使用您的 id 字段填充它。当用户提交表单时,模型绑定器将拾取该字段,并自动将其放入
tumourSubgroupToCreate
的 id 属性中。Assuming that
TumourGroup
is the name of a controller, and you have a route that looks something like this:Then in your
TumourGroup
controller, you just need a controller method that looks like this:The parameter id will contain your id from the Url.
EDIT: To include the id when you are submitting a form:
Add the id as a property to your
TumorGroupSubcategory
class.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.