我正面臨著一個問題,你們中的大多數人(包括我)都會通過給表單放置一個隱藏的輸入來立即解決,但這次我幾乎什么都試過了。我想通過添加一個SolarPanelType物體來修改一個InputForm物體。由于其他選項的負載,我使用了viewmodels嵌入,你可以看到
。public class InputFormViewModel
{
public InputForm InputForm { get; set; }
public IEnumerable<TetoTipus> TetoTipusEnumerable { get; set; }
public IEnumerable<TetoFedes> TetoFedesEnumerable { get; set; }
public IEnumerable<TetoDoles> TetoDolesEnumerable { get; set; }
public IEnumerable<NapelemElhelyezes> NapelemElhelyezesEnumerable { get; set; }
public IEnumerable<NapelemTipus> NapelemTipusEnumerable { get; set; }
public class ModifyInputFormViewModel<
{
public InputFormViewModel InputFormViewModel;
public SelectList SolarPanelTypesList { get; set; }
然后我在我的控制器中這樣初始化它們
public async Task< IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound()。
}
ModifyInputFormViewModel modifyInputFormViewModel = new()
{
InputFormViewModel = new InputFormViewModel
{
InputForm = await _inputFormService.FindInputFormAsync(id)。
TetoTipusEnumerable = await _inputFormService.GetTetoTipusokAsync()。
TetoFedesEnumerable = await _inputFormService.GetTetoFedesekAsync()。
TetoDolesEnumerable = await _inputFormService.GetTetoDolesekAsync()。
NapelemElhelyezesEnumerable = await _inputFormService.GetNapelemElhelyezesekAsync()。
NapelemTipusEnumerable = await _inputFormService.GetNapelemTipusokAsync()
},
SolarPanelTypesList = new SelectList(await _solarPanelTypeService. GetSolarPanelTypesAsync(), "ID"/span>, "DisplayString"/span>)
};
if (modifyInputFormViewModel.InputFormViewModel.InputForm == null)
{
return NotFound()。
}
return View(modifyInputFormViewModel)。
而POST方法
[HttpPost, ValidateAntiForgeryToken, Authorize]
public async Task< IActionResult> Edit(int id, ModifyInputFormViewModel modifyInputFormViewModel)
{
if (id != modifyInputFormViewModel.InputFormViewModel.InputForm.ID)
{
return NotFound()。
}
if (ModelState.IsValid)
{
await _inputFormService.ModifyInputFormAsync(modifyInputFormViewModel.InputForm)。
return RedirectToAction(nameof(Index))。
}
return View(modifyInputFormViewModel);
最后,這里是編輯視圖的關鍵部分
<form asp-action="Edit"/span>>
<div asp-validation-summary="ModelOnly" class="text-danger"> < /div>
<input type="hidden" asp-for="InputFormViewModel.InputForm.ID" />
<div class="form-group">
<標簽asp-for="InputFormViewModel.InputForm.SolarPanelType" class="controll-label"> </label>
<select asp-for="InputFormViewModel.InputForm.SolarPanelTypeID" asp-items="@Model. SolarPanelTypesList" class="form-control"> </select>
</div>
<div class="form-group">
<標簽asp-for="InputFormViewModel.InputForm.MonthlyElectricityBill" class="controll-label"> </label>
<input asp-for="InputFormViewModel.InputForm.MonthlyElectricityBill" class="form-control" />
<span asp-validation-for="InputFormViewModel.InputForm.MonthlyElectricityBill" class="text-danger"> < /span>
</div>
<section id="TetoTipusSection">
<h5>@Html.DisplayNameFor(model => model.InputFormViewModel.InputForm.TetoTipus)</h5>
<p>
@foreach (TetoTipus tetoTipus in Model.InputFormViewModel.TetoTipusEnumerable)
{
<標簽>
<input type="radio" asp-for="InputFormViewModel. InputForm.TetoTipusID" value="@tetoTipus.ID" /> @tetoTipus.Name
</label>
<br />
}
</p>
<hr />
</section>
[...]
</form>
最后一個重要的資訊是,視圖的渲染完全是我想要的,隱藏的輸入正確地包含了InputForm.ID,但隨后post方法收到了NULL值。
uj5u.com熱心網友回復:
像下面這樣更新你的代碼:-
<form asp-action="Edit"/span> method="post"/span> enctype="multipart/form-data"/span>>
和你的控制器:-
[HttpPost]
public async Task< IActionResult> Edit(int? ID){...}。
希望它能解決你的問題。
uj5u.com熱心網友回復:
修復視圖,因為你使用AntiForgeryToken,所以把它添加到表單中
<form asp-action="Edit"/span> method="post"/span>>
@Html.AntiForgeryToken()
和動作輸入引數應該和模型一樣,去掉id
。 public async Task< IActionResult> Edit( ModifyInputFormViewModel modifyInputFormViewModel)
{
var id= modifyInputFormViewModel.InputFormViewModel.InputForm.ID。
.......
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/322493.html
標籤:
