我有一个包含多个锚标签的视图。有没有办法使用模型对象返回视图并转到视图中的特定锚标记?
例如,我的View有这样的锚:
<a name="Section1"></a>
...
<a name="Section2"></a>
我知道我可以使用以下方法击中这些锚:
return Redirect(Url.RouteUrl(new { controller = "myController", action = "myAction" }) + "#Section1");
但我不认为我可以使用重定向,因为我需要发送一个模型:
return View("myAction", model); // how to go to anchor?
答案 0 :(得分:12)
您可以在视图模型中发送内容以查看并使用javascript滚动到该锚点。例如,假设您有一个名为Section的属性。您可以在控制器中设置它,并在视图中使用此javascript代码滚动到该锚点:
$(document).ready(function () {
var anchor = document.getElementById('@Model.Section');
anchor.scrollIntoView(true);
});
答案 1 :(得分:2)
首先,我们需要将achor传递给我们的观点:
<强>控制器:强>
ViewBag.Section = "register"; //#register
return View();
查看:强>
@if (ViewBag.Section!=null)
{
<script>
$(function () {
window.location.hash = '#@ViewBag.Section';
});
</script>
}
现在您可以使用&#34;如何滚动到锚点&#34;回答https://stackoverflow.com/a/15906458/7149454