我正在尝试使用该ViewComponents.InvokeAsync()功能,但不知何故,这根本不是异步.它正在等待组件代码呈现.
http://docs.asp.net/en/latest/mvc/views/view-components.html
我的代码与上面例子中解释的代码非常相似.我正在使用在MVC 6中创建新应用程序时出现的布局页面.
我认为该ViewComponent.InvokeAsync()方法将相对于主页面异步呈现.但事实并非如此.为了实现这一点,我们需要使用这里解释的AJAX .
我正在构建一个小型 Web 应用程序来测试新的 ASP.NET Core MVC。我实现了一个包含登录表单的视图组件,我需要将其插入到整个应用程序的各个位置。我的表单当前将数据发布到处理登录的普通控制器。
现在考虑到用户提供了错误的登录信息,我想向模型状态添加一条错误消息并将其显示在我的表单下,就像正常验证一样。但是问题是view组件可以集成到各种地方,我怎么知道form post是从哪里来的呢?
我想呈现用户用来再次输入他的凭据的同一个站点,只有验证错误。有没有办法找出表单帖子来自哪个视图?或者更好的是有什么标准方法可以将表单作为 ASP.NET Core MVC 中的视图组件处理?
我正在为MVC .NET Core Web应用程序使用最新的VS.2017更新和模板。我决定在外部程序集中使用ViewComponents,因为我读了几篇文章,指出没有奇数技巧是不可能的。
我有主要的Web应用程序,然后创建了一个名为MySite.Components的.NET Framework类库,它是“外部程序集”。在其中安装了ViewFeatures NuGet。我在其/Views/Shared/Components/GoogleAdsense/Default.cshtml中创建了View组件CSHTML。
我注意到CSPROJ已经将GoogleAdSense作为嵌入式资源:
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<EmbeddedResource Include="Views\Shared\Components\GoogleAdsense\Default.cshtml" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
视图组件实际上非常简单:
namespace MySite.Components.ViewComponents {
[ViewComponent(Name = "GoogleAdsense")]
public class GoogleAdsense : ViewComponent {
public async Task<IViewComponentResult> InvokeAsync(string adSlot, string clientId, string adStyle = "")
{
var model = await GetConfigAsync(adSlot, clientId, adStyle);
return View(model);
}
private Task<GoogleAdUnitCompModel> GetConfigAsync(string adSlot, string clientId, string adStyle)
{
GoogleAdUnitCompModel model = new GoogleAdUnitCompModel
{
ClientId = clientId, // apparently we can't …Run Code Online (Sandbox Code Playgroud) 我正在编写一个自定义 TagHelper,并希望在其中呈现一个 ViewComponent。类似于 vc:xyz 标签助手所做的事情,但以更可控的方式,以便我可以在运行时确定要呈现哪个 ViewComponent。
是否可以?
c# asp.net-core asp.net-core-tag-helpers asp.net-core-viewcomponent
我目前有这个看法:
但是每当我向购物车添加东西时,它都不会自动更新计数。仅当我刷新页面时。
我怎样才能让它自动更新?它在 _Layout 视图上带有一个视图组件。
购物车视图组件
public class CartViewComponent : ViewComponent
{
private readonly ApplicationDbContext _context;
private readonly SignInManager<UserModel> _signInManager;
private readonly UserManager<UserModel> _userManager;
public CartViewComponent(
ApplicationDbContext context,
SignInManager<UserModel> signInManager,
UserManager<UserModel> userManager)
{
_context = context;
_signInManager = signInManager;
_userManager = userManager;
}
public int CheckIfOrderExists()
{
var userId = Request.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
OrderMain orderMain = _context.OrderMains.Where(o => o.user_id == userId).FirstOrDefault();
if (orderMain != null)
{
return orderMain.id;
}
else
{
return 0;
}
}
public int CountItemsInShoppingCart(int order_id)
{
var result …Run Code Online (Sandbox Code Playgroud) 我目前有几个在许多不同地方使用的视图组件。我希望能够将调用视图组件时生成的 HTML 存储到字符串变量中,以便我可以将 HTML 用于其他目的(例如电子邮件)。
我查看了问题和帖子,但无法找到符合我需求的问题和帖子。我目前已存储 ViewComponentResult 供使用,但 ExecuteResult() 返回 void,因此无法使用它。
我希望能够操纵要执行的视图组件结果,然后返回将发送到浏览器进行显示的 HTML。然后我可以使用它作为电子邮件正文。然而,如果不知道结果视图的完整路径,我目前还无法接近获取 HTML
我试图使用javascript调用ViewComponent来促进页面加载,现在我使用razor调用了viewcomponent但是页面加载需要很长时间,怎么做?
这是控制器中的方法:
public async Task<IViewComponentResult> InvokeAsync(string id)
{
//var notifications = _context.Notifications.Include(m => m.ApplicationUser).Include(m => m.Sender).OrderByDescending(m => m.Id);
return View(await _context.Follows.Include(f=>f.User.University).Include(f=>f.User.Faculty).Where(n=>n.FollowedUserId==id).ToListAsync());
}
Run Code Online (Sandbox Code Playgroud)
和方法调用(jQuery):
<script>
$(document).ready(function () {
$('#viewallfollowers').click(
function () {
$("#followersrefresh").load("@await Component.InvokeAsync("Follower", new { id=Model.ApplicationUser.Id })");
});
});
</script>
Run Code Online (Sandbox Code Playgroud) 我是 ASP.NET Core 新手。当我使用视图组件时出现此错误。感谢您的帮助
InvalidOperationException:视图组件“UI.ViewComponents.NavbarViewComponent”的方法“Invoke”无法返回任务。
这是我的代码
public async Task<IViewComponentResult> Invoke()
{
var settings = await _db.Settings.ToListAsync();
return View(settings);
}
Run Code Online (Sandbox Code Playgroud)
并认为:
@await Component.InvokeAsync("Navbar")
Run Code Online (Sandbox Code Playgroud) 我在某个页面中使用ViewComponent作为渲染侧边栏.我需要当前登录用户进行某些操作,如何在ViewComponent中获取用户?当然我可以作为参数从视图传递到InvokeAsync方法,但我正在寻找更好的方法.
public class SideBarViewComponent : ViewComponent
{
private readonly UserManager<User> _userManager;
public ProfileSideBarViewComponent(UserManager<User> userManager)
{
_userManager = userManager;
}
public async Task<IViewComponentResult> InvokeAsync()
{
//How access User
_userManager.GetUserId(User);
return View();
}
}
Run Code Online (Sandbox Code Playgroud) 为了实现下面图片的布局,我想询问有关页面布局体系结构的最佳实践。在页面的每个部分或部分视图中拥有独立的ViewComponent更好吗?是否可以嵌套ViewComponents?
这个想法是在不同页面的其他位置重用这些部分。这个概念与我们以前拥有的Web部件非常相似,但是现在我尝试使用Asp来实现类似的东西。网络核心。
这段代码可以很好地从控制器中获取会话 ID:
HttpContext.Session.SetString("_Name", "MyStore");
string SessionId = HttpContext.Session.Id;
Run Code Online (Sandbox Code Playgroud)
...但是当我尝试将相同的代码放在视图组件类中时,VS 告诉我名称HttpContext.Session.SetString(或只是HttpContext.Session,或只是HttpContext)在当前上下文中不存在。我using Microsoft.AspNetCore.Http;在班上名列前茅。
编辑
这是我的视图组件类:
public class ShoppingCartViewComponent : ViewComponent
{
private readonly MyStoreContext _context;
public ShoppingCartViewComponent(MyStoreContext context)
{
_context = context;
}
// Initialize session to enable SessionId
// THIS WON'T WORK:
HttpContext.Session.SetString("_Name", "MyStore");
string SessionId = HttpContext.Session.Id;
public async Task<IViewComponentResult> InvokeAsync(int Id)
{
var cart = await GetCartAsync(Id);
return View(cart);
}
private async Task<ViewModelShoppingCart> GetCartAsync(int Id)
{
var VMCart = await _context.ShoppingCarts …Run Code Online (Sandbox Code Playgroud) 在ASP.NET 5 MVC6 RC1中-我有一个ViewComponent,旨在代表我的传统“屏幕左侧”主菜单。
我正在编写我的第一个TagHelper来代表每个菜单项链接。
我被困在试图创建超链接的部分。
我该如何解决 ~/dashboard/summary?
如果我在此页面上显示菜单,则链接显示为/dashboard/~/dashboard/summary。
@Url.Content("...")显示@Url.Content("...")即未作为剃刀处理。标记帮助器输出pure。
理想情况下,我希望该解决方案与.NET Core兼容,因为我最终的目标是.NET Core可部署解决方案。
见下文:
namespace Website
{
/// <summary>
/// <MainMenuLink area="" controller="" action=""></MainMenuLink>
///
/// to render
///
/// <a href="~/account/manage/ChangePassword" class="list-group-item @GetClassName("manage", "changepassword")">
/// <p class="list-group-item-text"><i class="fa fa-terminal"></i> Change my password</p>
/// </a>
///
///
/// </summary>
[HtmlTargetElement(Attributes = "area, controller, action")]
public class MainMenuLinkTagHelper : TagHelper
{
[HtmlAttributeName("area")]
public string Area { get; set; }
[HtmlAttributeName("controller")]
public string Controller { get; set; } …Run Code Online (Sandbox Code Playgroud) c# asp.net-core asp.net-core-tag-helpers asp.net-core-viewcomponent
什么是调用方法?
下面是我的viewcomponent()方法。
public IViewComponentResult Invoke()
{
ViewBag.SelectedCategory = RouteData?.Values["category"];
return
View(repository.Products.Select(x=>x.Category).Distinct().OrderBy(x=>x));
}
Run Code Online (Sandbox Code Playgroud)
这是匿名函数吗?
这个函数的名字是什么?
我试图用“func”更改“Invoke”,但没有语法错误,但发生了运行时错误。
是否可以在单个视图组件类中具有多个具有“调用”名称并接收不同参数的方法。
我知道视图组件用于为此呈现部分视图,我们使用 helper 就是这个
@await Component.InvokeAsync("Here is the name of your view component")
Run Code Online (Sandbox Code Playgroud)
"component.InvokeAsynk"去你的组件类,找到 Invoke 方法,然后按照这个方法
asp.net-core-viewcomponent ×13
asp.net-core ×10
c# ×9
razor ×3
ajax ×1
asp.net-mvc ×1
html ×1
jquery ×1
session ×1