我有两个问题:
1.什么是Viewcontext以及它的优点是什么?
2.为什么我们必须在标签助手中使用它?
实际上我是初学者,并遵循adam freeman的"Pro ASP.NET Core MVC,第6版",他在这里制作了一个taghelper类,他使用了
[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext { get; set; }
Run Code Online (Sandbox Code Playgroud)
他没有解释上面这段代码为什么他在这些属性的方括号中使用这些属性.如果有的话,请分享描述这些类型属性的链接
asp.net-mvc viewcontext asp.net-core-mvc asp.net-core-tag-helpers
我想创建一个静态帮助方法,我可以从视图中调用.
辅助方法是否可以访问当前的ViewContext而无需将ViewContext作为参数显式传递给方法?
类似于HttpContext.Current,除了ViewContext.
我的项目中有一个这样的控制器:
class BriefcasesController < ApplicationController
...
def update
@super_power = SuperPower.find(params[:super_power_id])
@briefcase.contents.delete(params[:super_power_id].to_s)
flash[:notice] = "Successfully removed #{view_context.link_to(@super_power.title, super_power_path(@super_power)} from your briefcase."
redirect_back(fallback_location: '/briefcase'
end
end
Run Code Online (Sandbox Code Playgroud)
帮助link_to器不会渲染到浏览器的链接,而是打印 html:Successfully removed <a href=\"/powers/1\>flying</a> from your briefcase.我也尝试过#html_safe在 flash 消息上使用该方法,但无济于事。我想知道是否有一种方法可以解决此问题view_context,或者是否有更好的方法可以在 Flash 消息中包含链接。