我想创建一个标签助手来在<input>标签之前和之后放置一些 HTML,但我想保留默认asp-for行为并访问ModelExpression数据。
为此,我尝试重写该Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper类型,如这篇文章中所述。然而,即使我可以在调试器中验证该Process方法正在被调用PreContent并且PostContent都已被设置,但除了标准标记之外,HTML 中没有显示任何内容<input>。不过,当从头开始为另一个标签创建标签助手时,它工作得很好。
我创建了一个小项目来演示这个问题。我将整个项目放在 GitHub 上,并复制了我在下面尝试创建的特定标签助手。
[HtmlTargetElement("input", Attributes = "asp-for,test-label")]
public class TestTagHelper : InputTagHelper
{
public TestTagHelper(IHtmlGenerator generator) : base(generator)
{
}
[HtmlAttributeName("test-label")]
public string Label { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.PreContent.SetHtmlContent($"<b>{WebUtility.HtmlEncode(Label)}</b> ");
output.PostContent.SetHtmlContent($" <i>({WebUtility.HtmlEncode(For.Name)})</i>"); // access information from the input tag
base.Process(context, output);
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗?这样做有什么警告吗?或者这是根本无法完成的事情?
是的,我知道这个措辞很难理解,但这让我很烦恼.在最近的一个项目中,我有一个recurses函数,并且有许多条件会导致它停止递归(目前为三).哪种情况可选?(IE最佳性能或最简单的维护).
1)有条件退货:
void myRecursingFunction (int i, int j){
if (conditionThatWouldStopRecursing) return;
if (anotherConditionThatWouldStopRecursing) return;
if (thirdConditionThatWouldStopRecursing) return;
doSomeCodeHere();
myRecursingFunction(i + 1, j);
myRecursingFunction(i, j + 1);
}
Run Code Online (Sandbox Code Playgroud)
2)用if语句包装整个东西
void myRecursingFunction (int i, int j){
if (
!conditionThatWouldStopRecursing &&
!anotherConditionThatWouldStopRecursing &&
!thirdConditionThatWouldStopRecursing
){
doSomeCodeHere();
myRecursingFunction(i + 1, j);
myRecursingFunction(i, j + 1);
}
}
Run Code Online (Sandbox Code Playgroud)
3)你做错了noob,没有理智的算法会使用递归.
我试图在win32中编写hello world但是当我关闭主窗口时,应用程序继续运行
我的窗口程序:
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wparam, lparam);
}
Run Code Online (Sandbox Code Playgroud)
事件循环:
while ((bret = GetMessage(&msg, hWndMain, 0, 0) != 0)
{
if (bret == -1)
{
DWORD error = GetLastError();
return 1;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Run Code Online (Sandbox Code Playgroud)
现在,通过单击右上角x得到WM_DESTROY,而不是GetMessage()返回0表示获取WM_QUIT,而是返回-1而GetLastError()发出错误1400,这是"无效窗口句柄" ......我很困惑.
考虑我有两个数组:
$friends = Array('foo', 'bar', 'alpha');
$attendees = Array('foo', 'bar');
Run Code Online (Sandbox Code Playgroud)
现在我需要填充新的数组$nonattendees只包含它们中的元素$friends数组,并没有在$attendees数组中.即,$nonattendees数组应填充'alpha'.
PHP中是否有可用的构建数组操作来实现上述功能,还是应该编写自己的for循环?
几个月来,我一直在考虑下载微软的快速网络平台和学习ASP.NET,我可能真的很喜欢,因为我已经使用PHP进行网络工作了,但是对C#更加满意.
但是,我不想这样做的主要原因是我总是将ASP.NET与无用的意大利面条HTML相关联.我发布的链接就是一个很好的例子.是否可以在更类似于PHP的上下文中使用ASP.NET,使用它来为我的网站提供动力,但是为了验证和语义原因,不要手动完成HTML,CSS和JavaScript?
编辑
我已经决定不学习ASP.NET并坚持使用PHP.
虽然MVC听起来不错,但对我来说,它可能最终会成为开发/调试的头疼问题.
arrays ×1
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
c ×1
c# ×1
php ×1
razor-pages ×1
recursion ×1
tag-helpers ×1
webforms ×1
winapi ×1