我有一个以下的模型属性,其中一个是projectDescription,它ntext在SQL Server 中生成一个列.
型号:
--some other properties here....
[Display(Name = "Project Title")]
[Column(TypeName = "varchar(125)")]
public string ProjectTitle { get; set; }
[Column(TypeName = "ntext")]
public string ProjectDesctiption { get; set; }
--some other properties here....
Run Code Online (Sandbox Code Playgroud)
此属性绑定到a中的textarea标记,View如下所示.但是,它不是显示ProjectDesctiption来自Db列的真实数据,而是奇怪地显示了查看页面的整个html源页面 - 如下图所示.其他标签正确显示数据,如下图所示的ProjectTitle字段中所示.我认为这个问题与ntext数据类型和textarea的ASP Tag助手有关.我确实在数据库中检查了ProjectDescription中某些数据单元的最大长度是非常大的 - 大约61968个字符.但我们确实希望使用水平和垂直滚动条以合理的textarea长度和宽度显示此列中的数据,这样用户至少可以浏览该字段的数据,以了解项目描述是什么(例如)或可能出于某种目的从textrea复制/过去数据.问题:如何实现这一目标?
查看:
---some html here.....
<div class="form-group">
<label asp-for="ProjectTitle" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ProjectTitle" class="form-control" />
<span asp-validation-for="ProjectTitle" class="text-danger"></span>
</div>
</div>
<div …Run Code Online (Sandbox Code Playgroud) ASP.NET 核心。
<label asp-for="FooModelProperty">...</label>
Run Code Online (Sandbox Code Playgroud)
这会生成一个具有适当属性名称的表单控件。选择、输入等也是如此。
但我需要在某个随机元素上使用该名称,例如 a div,这样我就可以在 JavaScript 中使用它。问题是asp-for标签助手不适用于任意元素。
那么我该怎么做这样的事情:
<div asp-for="FooModelProperty">...</div>
Run Code Online (Sandbox Code Playgroud) 在 asp.net core 标记帮助器上设置属性的方式如下:
[HtmlTargetElement("test1",Attributes = "make",ParentTag = "myparent")]
Run Code Online (Sandbox Code Playgroud)
我不明白语法Attributes = "make"
我知道 Attributes 是一个传递到构造函数中的字符串HtmlTargetElement,但我没有得到的是该= "make"部分。如果这是我现在的方法的调用列表,则意味着 make 是传入 null 的值,但当它位于调用方法端时我不明白它。
属性中的 Attributes 属性有何用途HtmlTargetElement?
我在asp.net MVC源代码中找不到TagHelper方法的源代码.我想了解两者之间的关系.我注意到ITagHelper接口只需要ProcessAsync,所以我认为Process必须以某种方式调用ProcessAsync,但我想在源代码中找到它,所以我更好理解.
https://docs.microsoft.com/en-us/aspnet/core/api/microsoft.aspnetcore.razor.taghelpers.itaghelper
我想触发股票ScriptTagHelper(在 GitHub 上查看源代码),以便它模拟该asp-append-version="true"属性。
我知道使用它的正确方法是改变这个:
<script src="somefile.js"></script>
Run Code Online (Sandbox Code Playgroud)
对此:
<script src="somefile.js" asp-append-version="true"></script>
Run Code Online (Sandbox Code Playgroud)
这个过程与版本控制 CSS 包含和图像(LinkTagHelper和ImageTagHelper)非常相似。
由于我有很多包含的脚本、样式表和图像,我想自动化一些事情。因此asp-append-version="true",与其添加每个 HTML 元素,不如创建一个自定义 TagHelper 来为我执行此操作。
这就是问题所在 - 它不起作用。
目前,我的 TagHelper 仅涵盖script标签,如下所示:
[HtmlTargetElement("script", Attributes = "src")]
public class TestTagHelper : TagHelper
{
public override int Order => int.MinValue;
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if(!context.AllAttributes.ContainsName("asp-append-version"))
{
output.Attributes.SetAttribute("asp-append-version", "true");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它不会触发默认值ScriptTagHelper,而是直接将 输出
asp-append-version="true"到输出 HTML。我还将该Order属性设置为 INT_MIN,以便它在任何其他标签助手之前触发,但它仍然不起作用。
有没有办法使这项工作?
c# asp.net-core-mvc tag-helpers asp.net-core asp.net-core-2.0
我是 ASP.NET Core 的新手。我刚刚发现了 TagHelpers ,正如我可能明白的那样,理论上我们应该能够用tag helpers替换部分视图。
除此之外,TagHelper可以接受输入但PartialView不接受。
我觉得对吗?还是误会?任何人都可以清楚地解释其中的区别吗?
提前致谢!
我想创建一个标签助手来在<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)
我错过了一些明显的东西吗?这样做有什么警告吗?或者这是根本无法完成的事情?
我最近更新了Visual Studio forUpdate 3和ASP.Net Core for 1.0.0。
我按照文档中的教程进行操作,并尝试设置使用像这样的区域https://docs.asp.net/en/1.0.0/mvc/controllers/areas.html
但是,生成的链接是http://localhost:2187/?area=Admin,而不是http://localhost:2187/Admin/Home/Index
更新
我的路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "areaRoute",
template: "{area}/{controller=Home}/{action=Index}");
});
Run Code Online (Sandbox Code Playgroud)
怎么了?
解决方案
问题在于答案中提到的路线顺序。
我在一个实例中使用了 asp-route-id:
它从这样的锚标签工作:
<a id="editModal" data-toggle="modal" data-target="#modal_box"
class="btn btn-sm btn-primary"
asp-action="Edit" asp-route-id="@user.Id">Edit</a>
Run Code Online (Sandbox Code Playgroud)
并像这样收到:
public async Task<PartialViewResult> DeleteConfirm(string Id) =>
PartialView("_DeleteConfirm", await _userManager.FindByIdAsync(Id));
Run Code Online (Sandbox Code Playgroud)
此方法接收正确的 id。
下一个示例在表单标签中包含路由 id,如下所示:
<div class="modal-footer">
<form asp-controller="Account" asp-action="Delete" asp-route-id="@Model.Id" method="post">
<button type="submit" class="btn btn-primary"
asp-action="Delete">Yes - Delete this User</button>
<a asp-action="Index" class="btn btn-default">Cancel</a>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
我试图在这里接收 ID:
[HttpPost]
public async Task<IActionResult> Delete(string Id)
{
AppUser user = await _userManager.FindByIdAsync(Id);
if (user != null)
{
IdentityResult result = await _userManager.DeleteAsync(user);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
else
{ …Run Code Online (Sandbox Code Playgroud) 如何阻止我的 TagHelper 渲染外部标签?
例如我想要类似的东西:
<title>
<translate>LoginPageTitle</translate>
</title>
Run Code Online (Sandbox Code Playgroud)
...因此标签助手可以从数据库获取翻译并渲染:
<title>Login to Foobar</title>
除了重命名原始标签之外,我根本不知道如何做任何事情,但我根本不需要任何标签。
这是我当前的代码:
public class TranslateTagHelper : TagHelper
{
public string ResourceSet { get; set; } = "Global";
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var content = await output.GetChildContentAsync();
string originalContent = content.GetContent();
output.Content.SetContent(@DbRes.T(originalContent, ResourceSet)); // Get translation from DB
}
}
Run Code Online (Sandbox Code Playgroud) asp.net-core-mvc tag-helpers asp.net-core-tag-helpers asp.net-core-3.0