I don't want to use @html.Action and @Rendersection can any one guide me what i use
我不想使用@html.Action和@Rendersection可以任何人指导我使用的
i have a menu there is lot of companies coming in <li>
it is fetching from DB
我有一个菜单,有很多公司进来
the menu should show on every page and it fetch from Db.some one told me to create a Class
菜单应显示在每个页面上,并从Db.some中获取一个告诉我创建一个类
but i don'thave idea what he want to say
但我不知道他想说什么
public PartialViewResult FeaturedStoresMenu()
{
var model = _context.companyService.GetFeaturedStores();
return PartialView(model);
}
@RenderSection("FeaturedCategoriesMenu", false)
</li>
<li><a class="MenuBarItemSubmenu" href="#">Coupons</a>
<ul>
@Html.Action("FeaturedStoresMenu", "Home")
</ul>
</li>
yes on many pages but i don't want to use "RenderSection" and "html.Action" is there any other thing which i can use.....Because if i use @renderSection so i have to pass List from every controller to View to show my Companies....
在许多页面上是的但是我不想使用“RenderSection”和“html.Action”还有其他任何我可以使用的东西.....因为如果我使用@renderSection所以我必须从每个控制器传递List查看以显示我的公司....
2 个解决方案
#1
2
you need to render the collection of your menu sub items in partial view.
您需要在局部视图中渲染菜单子项的集合。
Action
行动
public PartialViewResult FeaturedStoresMenu()
{
var model = _context.companyService.GetFeaturedStores();
return PartialView(model);
}
View = FeaturedStoresMenu (partial)
View = FeaturedStoresMenu(部分)
@model YourAppNamespace.YourModelType //type is collection for example List<SumenuItem>
<ul>
@foreach(var item in Model)
{
<li ><a href="@item.SubmenuUrl">@item.SubmenuName</a></li>
}
</ul>
And if u need pass the parameter to your child action like this
如果你需要将参数传递给你的子动作,就像这样
@Html.Action("FeaturedStoresMenu", "Home", new {id = 555})
action should be
行动应该是
public PartialViewResult FeaturedStoresMenu(long id)
{
var model = _context.companyService.GetFeaturedStores(id);
return PartialView(model);
}
Or u can write custom Html helper method and write the menu logic in this method. example:
或者你可以编写自定义的Html辅助方法并在此方法中编写菜单逻辑。例:
public static class LabelExtensions
{
public static string Label(this HtmlHelper helper, string target, string text)
{
return String.Format("<label for='{0}'>{1}</label>", target, text);
}
}
from here
从这里
#2
0
Make a PartialView which contains your menu, Put it in Views/Shared folder
创建一个包含菜单的PartialView,将其放入Views / Shared文件夹中
then put it on any page where you want it like
然后把它放在你想要的任何页面上
@Html.Partial("NameofView")
#1
2
you need to render the collection of your menu sub items in partial view.
您需要在局部视图中渲染菜单子项的集合。
Action
行动
public PartialViewResult FeaturedStoresMenu()
{
var model = _context.companyService.GetFeaturedStores();
return PartialView(model);
}
View = FeaturedStoresMenu (partial)
View = FeaturedStoresMenu(部分)
@model YourAppNamespace.YourModelType //type is collection for example List<SumenuItem>
<ul>
@foreach(var item in Model)
{
<li ><a href="@item.SubmenuUrl">@item.SubmenuName</a></li>
}
</ul>
And if u need pass the parameter to your child action like this
如果你需要将参数传递给你的子动作,就像这样
@Html.Action("FeaturedStoresMenu", "Home", new {id = 555})
action should be
行动应该是
public PartialViewResult FeaturedStoresMenu(long id)
{
var model = _context.companyService.GetFeaturedStores(id);
return PartialView(model);
}
Or u can write custom Html helper method and write the menu logic in this method. example:
或者你可以编写自定义的Html辅助方法并在此方法中编写菜单逻辑。例:
public static class LabelExtensions
{
public static string Label(this HtmlHelper helper, string target, string text)
{
return String.Format("<label for='{0}'>{1}</label>", target, text);
}
}
from here
从这里
#2
0
Make a PartialView which contains your menu, Put it in Views/Shared folder
创建一个包含菜单的PartialView,将其放入Views / Shared文件夹中
then put it on any page where you want it like
然后把它放在你想要的任何页面上
@Html.Partial("NameofView")