【问题标题】:How to pass a value into partial tag helper? (ASP.NET Core)如何将值传递给部分标签助手? (ASP.NET 核心)
【发布时间】:2020-05-16 07:32:36
【问题描述】:

我正在尝试在表单中呈现局部视图,我需要使用循环的值(当然是 +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>

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

【问题讨论】:

    标签: asp.net asp.net-core partial-views tag-helpers asp.net-core-tag-helpers


    【解决方案1】:

    我正在尝试在表单中呈现局部视图,我需要使用循环的值(当然是 +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>
    

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2021-05-20
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2018-06-11
      • 2019-11-13
      相关资源
      最近更新 更多