0

I am working in a .NET project with MVC and I need to make work a link that point to a section on the same page.

The following code works well without MVC:

<a href="#section1">Section 1</a>
<div id="section1">
  <h1>Section 1</h1>
</div>

Now this is my real URL: http://localhost:17338/MarketingCustomers/CleanData/1/1150119

And I need to be able to link with a div with id=customerErrorSection so the URL should looks like: http://localhost:17338/MarketingCustomers/CleanData/1/1150119#customerErrorSection

So I need to add "#customerErrorSection" at the end of the URL.

But the MVC routing changes the URL to http://localhost:17338/MarketingCustomers/CleanData/1/1150119#/customerErrorSection

I have been playing with the RouteConfig but I don't know how to create the URL I need, this is my code that isn't working:

routes.MapRoute(
                       name: "MarketingDataClean_CustomerErrorSection",
                       url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}/{#customerErrorSection}",
                       defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" }
                   );

Thanks for the help!

MarcosF8
  • 1,848
  • 5
  • 19
  • 33

4 Answers4

0

Try:

 routes.MapRoute(
  name: "MarketingDataClean_CustomerErrorSection",
  url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}/#customerErrorSection",
  defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" }
);
Ashiquzzaman
  • 5,129
  • 3
  • 27
  • 38
0

I don't really understand the problem here. Just leave the # part out.

routes.MapRoute(name: "MarketingDataClean_CustomerErrorSection",
                url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}",
                defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" });

And then use the URL: http://localhost:17338/MarketingCustomers/CleanData/1/1150119#customerErrorSection

If you want to use the Url.Action helper then use it like this:

@Url.Action("CleanData", "MarketingCustomers", new { systemSourceId = 1, systemSourceCustomerId = 1150119})#customerErrorSection
Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55
0

I am getting close now, the following code works fine in the View, but when I put it inside of a partial view does not work anymore.

<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>
MarcosF8
  • 1,848
  • 5
  • 19
  • 33
0

I found the problem,

Inside of other divs or panels, need to put to IDs of all of them:

<a href="#mainDiv#section2">Section 2</a>

Div 1: mainDiv, and Div 2: section2

Thanks again to all for the help!

MarcosF8
  • 1,848
  • 5
  • 19
  • 33