將具有禁用 html 屬性的輸入發送到更新物體時,input tag當Modelstate.IsValid處于閃爍狀態時,我遇到了空值。
我想更新一個名為Episode的物體。我的 ViewRazore 是:
<form method="post" enctype="multipart/form-data">
<input type="hidden" asp-for="@ViewData["EpisodeId"]" />
<input type="hidden" asp-for="@ViewData["EpisodeFileName"]" />
<h2>??????? ??????</h2>
<hr />
<div class="col-md-8">
<div class="danger" asp-validation-summary="All"></div>
<div class="form-group">
<label>??? ??????</label>
<input type="text" asp-for="@Model.Episode.EpisodeTitle" class="form-control">
<span asp-validation-for="@Model.Episode.EpisodeTitle"></span>
</div>
<div class="form-group">
<label>???? ??????</label>
<input type="datetime" asp-for="@Model.Episode.EpisodeTimeLength" class="form-control">
<span asp-validation-for="@Model.Episode.EpisodeTimeLength"></span>
</div>
<div class="form-group">
<label>Current EpisodeFileName</label>
<input disabled asp-for="@Model.Episode.EpisodeFileName" />
</div>
</div>
</form>
如您所見,我有以下代碼:
<div class="form-group">
<label>Current EpisodeFileName</label>
<input disabled asp-for="@Model.Episode.EpisodeFileName" />
</div>
我希望管理員在更新時看到Current EpisodeFileName(它可以是 mp4 檔案等)是什么。這是我的 EditEpisode.cshtml.cs:
public class EditEpisodeModel : PageModel
{
private ICourseServices _courseServices;
public EditEpisodeModel(ICourseServices courseServices)
{
_courseServices = courseServices;
}
[BindProperty]
public Episode Episode { get; set; }
public void OnGet(int id)
{
Episode = _courseServices.GetEpisodeByEpisodeId(id);
ViewData["EpisodeId"] = id;
ViewData["EpisodeFileName"] = Episode.EpisodeFileName;
}
public IActionResult OnPost(Episode episode)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("","???? ???? ???? ?? ?? ?? ??? ???? ????");
return Page();
}
return Page();
}
}
如您所見,當我的應用程式運行時,我決??定將其中一個輸入的值留空,例如我將以下輸入留空:
<input type="text" asp-for="@Model.Episode.EpisodeTitle" class="form-control">
如您所知,由于我在方法中的if 陳述句OnPost:
if (!ModelState.IsValid)
{
ModelState.AddModelError("","???? ???? ???? ?? ?? ?? ??? ???? ????");
return Page();
}
My OnPost method will call the ReturnPage().
My question is why when the ReturnPage() is called, the input <input disabled asp-for="@Model.Episode.EpisodeFileName" /> become empty!?
It's just that, when I use disabled attribute inside my input tag and then post the information, my input is became empty! But when I remove this attribute from the input, I didn't face with this issue !
So anybody can explain it to me ?
uj5u.com熱心網友回復:
默認情況下,瀏覽器不支持禁用屬性與表單一起提交:
disabled 和 readonly 之間的區別在于,只讀控制元件仍然可以運行并且仍然是可聚焦的,而禁用的控制元件不能獲得焦點并且不隨表單提交,并且通常在啟用之前不能用作控制元件。
對于您的方案,您可以更改disabled為readonly.
<input readonly asp-for="@Model.Episode.EpisodeFileName" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/324627.html
標籤:c# asp.net-core
上一篇:MySQL基于兩列的高級計算
