asp.net mvc的 - ActionLink的渲染不一致 [英] asp.net mvc - inconsistent rendering of ActionLink

查看:211
本文介绍了asp.net mvc的 - ActionLink的渲染不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,它接受以下两种格式的URL:

I have a controller which accepts URLs in the following two formats:


  • 网络/ AddOrEdit - >呈现一个页面上的空白表单添加一个新的网络对象

  • 网络/ AddOrEdit / [ID] - >渲染与prepopulated表单的页面编辑的网络对象
    与ID [ID]

  • Network/AddOrEdit -> renders a blank form on a page to add a new network object
  • Network/AddOrEdit/[id] -> renders a page with prepopulated form to edit the network object with ID [id]

显然,这是在每个实例中所使用的相同的视图 - 我的设计的目标之一是使用相同的视图用于添加和编辑的目的

Obviously it's the same view being used in each instance - one of my design goals is to use the same view for adding and editing purposes.

主页包含这样的链接添加页面:

The master page contains a link to the add page like this:

@Html.ActionLink("Add", "AddOrEdit", "Network")

按说这正确地呈现为 /网络/ AddOrEdit

然而,当我在编辑页面上(即当前URL的格式为网​​络/ AddOrEdit / [ID] ),然后添加链接呈现与上月底ID - 这样的添加链接实际指向编辑页面。这不是我想要的!

However, when I am on the edit page (i.e. the current URL is in the format Network/AddOrEdit/[id]), then the Add link renders with that ID on the end - so the add link actually points to the edit page. This is not what I want!

因此​​,对于某些原因MVC似乎允许从查询串的电流ID与ActionLink的的呈现干涉。

So for some reason MVC seems to be allowing the current ID from the query string to interfere with the rendering of the ActionLink.

任何建议,我该怎么办这件事? (

Any suggestions what I can do about this? :(

推荐答案

您猜测是正确的。 MVC路由机构可以从目前的请求重用路线变量以生成出局路由数据。这就是为什么 ID 参数当前请求填充。你应该明确地指定 ID 生成链接时

Your guessing is right. MVC routing mechanism may reuse route variables from the current request to generate outgoing route data. That's why the id parameter is populated from the current request. You should explicitely specify id when generating link

@Html.ActionLink("Add", "AddOrEdit", "Network", new { id = String.Empty }, null)

和路由系统看到带有可选的 ID 参数和路由值路由时与的String.Empty ,它生成到底没有 ID 链接

And when routing system sees route with optional id parameter, and route value with string.Empty, it generates link without id in the end

这篇关于asp.net mvc的 - ActionLink的渲染不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆