[英]How to pass a value into partial tag helper? (ASP.NET Core)
I'm trying to render a partial view inside a form, I need to use the value of the loop (+1 of course because it starts at 0) as one of the values in my partial, any ideas how I can make this work?我正在尝试在表单内呈现部分视图,我需要使用循环的值(当然是 +1,因为它从 0 开始)作为我的部分值之一,任何我如何使它工作的想法?
I've tried to do this with ViewData or ViewBag but either this is the wrong method or I'm implementing it wrong我尝试使用 ViewData 或 ViewBag 来执行此操作,但要么这是错误的方法,要么是我实施错误
Here is my main form:这是我的主要形式:
@model Measurement
<form asp-action="Create" method="post">
<div class="form-group" hidden>
<label asp-for="LymphSiteId"></label>
<input asp-for="LymphSiteId" value=@ViewBag.Id />
<span asp-validation-for="LymphSiteId"></span>
</div>
<div class="form-group" hidden>
<label asp-for="UserId"></label>
<input asp-for="UserId" value="1" />
<span asp-validation-for="UserId"></span>
</div>
<div class="form-group">
<label asp-for="MeasurementDate"></label>
<input asp-for="MeasurementDate" />
<span asp-validation-for="MeasurementDate"></span>
</div>
@for (int i = 0; i < ViewBag.NumMeasuringPoints; i++)
{
<partial name="_New" view-data=@(i+1) />
}
<button type="submit">Submit</button>
</form>
Here is my partial:这是我的部分:
@model Circumference
<div class="form-group" hidden>
<input asp-for="Id" />
</div>
<div class="form-group" hidden>
<input asp-for="MeasurementId" value="@ViewBag.Id" />
</div>
<div class="form-group">
<label asp-for="PositionFromStart">Position from Start</label>
<input asp-for="PositionFromStart" value="@ViewData" />
</div>
<div class="form-group">
<label asp-for="DistanceAround">Circumference at point (cm)</label>
<input asp-for="DistanceAround" />
</div>
Any help greatly appreciated - thanks!非常感谢任何帮助 - 谢谢!
I'm trying to render a partial view inside a form, I need to use the value of the loop (+1 of course because it starts at 0) as one of the values in my partial, any ideas how I can make this work?我正在尝试在表单中呈现局部视图,我需要使用循环的值(当然是 +1,因为它从 0 开始)作为我局部中的值之一,任何想法如何使这项工作?
You can try to modify the code like below to assigns a ViewDataDictionary to pass to your partial view.您可以尝试修改如下代码以分配一个 ViewDataDictionary 以传递给您的局部视图。
In main form在主要形式
@for (int i = 0; i < ViewBag.NumMeasuringPoints; i++)
{
ViewData["PositionFromStart"] = i + 1;
var pmodel = new Circumference();
<partial name="_New" model="pmodel" view-data="ViewData" />
}
In partial view部分观点
@model Circumference
<div class="form-group" hidden>
<input asp-for="Id" />
</div>
<div class="form-group">
<input asp-for="MeasurementId" value="@ViewBag.Id" />
</div>
<div class="form-group">
<label asp-for="PositionFromStart">Position from Start</label>
<input asp-for="PositionFromStart" value="@ViewData["PositionFromStart"]" />
</div>
<div class="form-group">
<label asp-for="DistanceAround">Circumference at point (cm)</label>
<input asp-for="DistanceAround" />
</div>
You can write your own MyPartialTagHelper by inheriting & extending the PartialTagHelper from .NET ( https://github.com/do.net/as.netcore/blob/main/src/Mvc/Mvc.TagHelpers/src/PartialTagHelper.cs )您可以通过从 .NET ( https://github.com/do.net/as.netcore/blob/main/src/Mvc/Mvc.TagHelpers/src/PartialTagHelper.cs ) 继承和扩展 PartialTagHelper 来编写自己的 MyPartialTagHelper
MyPartialHelper.cs MyPartialHelper.cs
[HtmlTargetElement("mypartial", Attributes = "name", TagStructure = TagStructure.WithoutEndTag)]
public sealed class MyPartialTagHelper
: Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper
{
private static readonly System.String[] SkipAttributeArray = new[]
{
"for", "model", "fallback-name", "optional", "name"
};
public MyPartialTagHelper(ICompositeViewEngine viewEngine, IViewBufferScope viewBufferScope)
: base(viewEngine, viewBufferScope)
{ }
private ViewDataDictionary getViewData([DisallowNull] TagHelperContext tagHelperContext)
{
if (tagHelperContext == null) throw new ArgumentNullException(nameof(tagHelperContext));
var viewData = new ViewDataDictionary(this.ViewContext.ViewData);
foreach (var attribute in tagHelperContext?.AllAttributes.Where(a => SkipAttributeArray.Contains(a.Name, StringComparer.InvariantCultureIgnoreCase) == false))
viewData.Add(attribute.Name, attribute.Value?.ToString());
return viewData;
}
public override void Process(TagHelperContext tagHelperContext, TagHelperOutput tagHelperOutput)
{
this.ViewData = getViewData(tagHelperContext);
base.Process(tagHelperContext, tagHelperOutput);
}
public override Task ProcessAsync(TagHelperContext tagHelperContext, TagHelperOutput tagHelperOutput)
{
this.ViewData = getViewData(tagHelperContext);
return base.ProcessAsync(tagHelperContext, tagHelperOutput);
}
}
Index.cshtml索引.cshtml
@addTagHelper *, MyAssembly
<mypartial name="the-partial" myproperty="myvalue" />
the-partial.cshtml部分.cshtml
@ViewData["myproperty"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.