首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >页面内的MVC链接

页面内的MVC链接
EN

Stack Overflow用户
提问于 2017-05-19 00:42:06
回答 4查看 334关注 0票数 0

我正在使用MVC在一个.NET项目中工作,我需要使工作指向同一页上的一个部分的链接。

以下代码在没有MVC的情况下也能正常工作:

代码语言:javascript
运行
复制
<a href="#section1">Section 1</a>
<div id="section1">
  <h1>Section 1</h1>
</div>

这是我的真实网址:http://localhost:17338/MarketingCustomers/CleanData/1/1150119

我需要能够链接到一个与id=customerErrorSection的div,所以网址应该如下所示:http://localhost:17338/MarketingCustomers/CleanData/1/1150119#customerErrorSection

因此,我需要在URL的末尾添加"#customerErrorSection“。

但是MVC路由将URL更改为http://localhost:17338/MarketingCustomers/CleanData/1/1150119#/customerErrorSection

我一直在玩RouteConfig,但我不知道如何创建我需要的网址,这是我的代码不工作:

代码语言:javascript
运行
复制
routes.MapRoute(
                       name: "MarketingDataClean_CustomerErrorSection",
                       url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}/{#customerErrorSection}",
                       defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" }
                   );

谢谢你的帮助!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-05-19 01:38:02

我不太理解这里的问题。去掉#部分就行了。

代码语言:javascript
运行
复制
routes.MapRoute(name: "MarketingDataClean_CustomerErrorSection",
                url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}",
                defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" });

然后使用以下网址:http://localhost:17338/MarketingCustomers/CleanData/1/1150119#customerErrorSection

如果你想使用Url.Action帮助器,那么就像这样使用它:

代码语言:javascript
运行
复制
@Url.Action("CleanData", "MarketingCustomers", new { systemSourceId = 1, systemSourceCustomerId = 1150119})#customerErrorSection
票数 0
EN

Stack Overflow用户

发布于 2017-05-19 01:35:27

尝试:

代码语言:javascript
运行
复制
 routes.MapRoute(
  name: "MarketingDataClean_CustomerErrorSection",
  url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}/#customerErrorSection",
  defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" }
);
票数 0
EN

Stack Overflow用户

发布于 2017-05-19 14:52:28

现在我已经接近了,下面的代码在视图中工作得很好,但是当我把它放在一个分部视图中时,它就不能再工作了。

代码语言:javascript
运行
复制
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section41">Section 4-1</a></li>
<li><a href="#section42">Section 4-2</a></li>
</ul>
<div id="section1">
 <h1>Section 1</h1>
</div>
<div id="section2">
 <h1>Section 2</h1>
</div>
<div id="section3">
 <h1>Section 3</h1>
</div>
<div id="section41" class="container-fluid">
 <h1>Section 4 Submenu 1</h1>
</div>
<div id="section42" class="container-fluid">
 <h1>Section 4 Submenu 2</h1>
 </div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44053538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档