如何在ASP.NET Core中将值传递给部分标签助手?

10

我试图在表单中呈现部分视图,我需要使用循环的值(+1,因为它从0开始)作为我的部分视图中的一个值,请问我该如何实现?

我已经尝试使用ViewData或ViewBag来实现,但是不管是方法错误还是我没有正确实现

这是我的主要表单:

@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>

这是我的部分内容:

@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>

非常感谢任何帮助 - 谢谢!

2个回答

13

我想在表单中呈现一个局部视图,我需要将循环的值(当然要加1,因为它从0开始)作为我的局部视图中的一个值。你有什么好的方法可以实现吗?

你可以尝试像以下代码一样修改,将ViewDataDictionary赋给局部视图。

在主表单中:

@for (int i = 0; i < ViewBag.NumMeasuringPoints; i++)
{
    ViewData["PositionFromStart"] = i + 1;

    var pmodel = new Circumference();

    <partial name="_New" model="pmodel" view-data="ViewData" />
}

部分显示

@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>

1
是否可以只传递一个字符串作为参数?例如 <partial name="_mypartial" Myproperty="optionA"/> - Enrico

2
你可以通过继承和扩展.NET的PartialTagHelper来编写自己的MyPartialTagHelper(https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.TagHelpers/src/PartialTagHelper.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

@addTagHelper *, MyAssembly

<mypartial name="the-partial" myproperty="myvalue" />   

the-partial.cshtml

@ViewData["myproperty"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接