扩展为<li>...</li>的方法表达 [英] Method that expands into <li>...</li> expression
本文介绍了扩展为<li>...</li>的方法表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在cshtml"文件中有以下内容:
Let's suppose I have the following in a 'cshtml' file:
<li class="nav-item">
<a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
我想设置一个方法 Link
以便我可以执行以下操作而不是上述操作:
I'd like to setup a method Link
so that I can do the following instead of the above:
@Link("nav-link text-white", "Home", "Index", "Home")
@Link("nav-link text-white", "Home", "Privacy", "Privacy")
这是我尝试过的一种方法:
Here's one approach that I tried:
@{
void Link(string a_class, string controller, string action, string text)
{
<li class="nav-item">
<a class="@a_class" asp-area="" asp-controller="@controller" asp-action="@action">@text</a>
</li>
}
}
但是,这会导致以下错误消息:
However, this leads the following error message:
实现上述Link
方法的好方法是什么?
What's a good way to implement the above Link
method?
推荐答案
让我假设您想要实现可重用性.如果是这样,那么您将有一个更好的选择来为此创建局部视图
Let me assume you want to achieve reusablility. If thats the case then you will have a better option of creating a partial view for that
这将是您的部分视图
@model MyPartialModel
<li class="nav-item">
<a class="@Model.Class" asp-area="@Model.Area" asp-controller="@Model.Controller" asp-action="@Model.Action">@Model.Text</a>
</li>
//Then you can call it anywhere
@await Html.PartialAsync("NameOfFile", new { Text ="whatever" Controller="MyController" Action=MyAction" className ="the-class"})
//Or even replace the anonymous object with a model rather
然后......就是这样
And.....that's it
这篇关于扩展为<li>...</li>的方法表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文