我有一个 Razor 模态,可以从 BE 获取数据
这是模型
public class LocationServiceEditDto: EntityDto<int?>
{
public int ServiceId { get; set; }
public int? LocationId { get; set; }
public decimal? Cost { get; set; }
public int? MaterialUomId { get; set; }
public string MaterialUomName { get; set; }
public string LocationName => Location?.Name;
[JsonIgnore]
public LocationNameDto Location { get; set; }
public ICollection<LocationServicePriceDto> LocationServicePrices { get; set; }
}
在BE我填充了
LocationServicePrices
的集合
然后在视图中,我这样显示
<div class="modal-body box-wrap">
<form role="form" novalidate class="form-validation" name="ServicePriceInformationsForm">
<input type="hidden" asp-for="Id"/>
<input type="hidden" asp-for="ServiceId"/>
<div class="form-group">
<label class="required-label">@L("Location")</label>
<select class="form-control" required asp-for="LocationId">
<option value="">@L("SelectALocation")</option>
@if (Model.LocationId.HasValue)
{
<option selected value="@Model.LocationId">@Model.LocationName</option>
}
</select>
</div>
<div class="form-group">
<label class="required-label">@L("MaterialUom")</label>
<select class="form-control" asp-for="MaterialUomId" id="ServicePrice_MaterialUomId">
<option value="">Select an option</option>
@if (Model.MaterialUomId > 0)
{
<option value="@Model.MaterialUomId">@Model.MaterialUomName</option>
}
</select>
</div>
<div class="form-group">
<label>@L("Cost")</label>
<input class="form-control" type="text" asp-for="Cost" data-rule-number="true" data-rule-min="0" data-rule-max="@AppConsts.MaxDecimalDatabaseLength">
</div>
@foreach (var tier in @Model.LocationServicePrices)
{
<div class="form-group">
<label>@tier.PricingTierName</label>
<input class="form-control" type="text" asp-for="@tier.PricePerUnit" data-rule-number="true" data-rule-min="0" data-rule-max="@AppConsts.MaxDecimalDatabaseLength">
</div>
}
</form>
</div>
我想更新 foreach 中的每个输入并更新
@Model.LocationServicePrices
。
目前,当我输入值并将数据发送到BE时,集合为空
我将这样的数据保存在
.js
文件中
this.save = function () {
if (!_$form.valid()) {
_$form.showValidateMessage();
return;
}
var servicePrice = _$form.serializeFormToObject();
_modalManager.setBusy(true);
_serviceService.editLocationService(servicePrice).done(function () {
abp.notify.info('Saved successfully.');
_modalManager.close();
abp.event.trigger('app.createOrEditServicePriceModalSaved');
}).always(function () {
_modalManager.setBusy(false);
});
};
我该如何解决这个问题?
不确定你的整个js代码是如何工作的,但是模型绑定系统通过名称绑定属性。由于
LocationServicePrices
是列表模式,您需要按名称传递它们,例如:name="LocationServicePrices[@i].PricePerUnit"
。
@{
int i = 0;
}
@foreach (var tier in @Model.LocationServicePrices)
{
<div class="form-group">
<label>@tier.PricingTierName</label>
<input class="form-control" type="text" asp-for="@tier.PricePerUnit"
name="LocationServicePrices[@i].PricePerUnit" data-rule-number="true" data-rule-min="0" data-rule-max="@AppConsts.MaxDecimalDatabaseLength">
</div>
i++;
}