如何将值传递到部分标签助手中?(ASP.NET Core) [英] How to pass a value into partial tag helper? (ASP.NET Core)
问题描述
我正在尝试在表单内渲染局部视图,我需要将循环的值(当然,+ 1是因为它从0开始)用作我的局部视图中的值之一,我想如何使这项工作吗?
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?
我尝试使用ViewData或ViewBag做到这一点,但这要么是错误的方法,要么是我实现错误了
I've tried to do this with ViewData or ViewBag but either this is the wrong method or I'm implementing it wrong
这是我的主要形式:
@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>
非常感谢任何帮助-谢谢!
Any help greatly appreciated - thanks!
推荐答案
我正在尝试在表单内渲染局部视图,我需要将循环的值(当然是+1,因为它从0开始)用作我的局部视图中的值之一,我想如何使这项工作吗?
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?
您可以尝试修改以下代码,以分配ViewDataDictionary传递给局部视图.
You can try to modify the code like below to assigns a ViewDataDictionary to pass to your partial view.
主要形式
@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>
这篇关于如何将值传递到部分标签助手中?(ASP.NET Core)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!