面臨如此奇特的問題。為了更好地理解,我將嘗試更詳細地描述。
我在 ArticleController 有兩個方法:
[HttpGet]
public async Task<IActionResult> Create()
{
var sources = await _sourceServices.GetSourceNameAndId();
var listSources = new List<SourceNameAndIdModel>();
foreach (var source in sources)
{
listSources.Add(_mapper.Map<SourceNameAndIdModel>(source));
}
var viewModel = new ArticleCreateViewModel()
{
SourceNameAndIdModels = listSources
};
return View(viewModel);
}
[HttpPost]
public async Task<IActionResult> Create(ArticleCreateViewModel viewModel)
{
await _articleService.CreateArticle(_mapper.Map<ArticleDTO>(viewModel));
return RedirectToAction("Index", "Article");
}
如您所見,在Get-method中,我通過_sourceService以IEnumerable的形式從資料庫中獲取所有 Sources 的名稱和 ID :
public class SourceNameAndIdDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
}
接下來,我在 foreach 回圈中列舉它們并將每個SourceNameAndIdDTO物件添加到我之前創建的List listSources中:
public class SourceNameAndIdModel
{
public string Name { get; set; }
public Guid Id { get; set; }
}
接下來,我創建ArticleCreateViewModel模型的一個實體,我將在View中進一步使用它:
public class ArticleCreateViewModel
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Title { get; set; }
public string Description { get; set; }
public string Body { get; set; }
public Guid SourceId { get; set; }
public DateTime CreationDate { get; set; }
public List<SourceNameAndIdModel> SourceNameAndIdModels { get; set; }
}
我分配給欄位public List SourceNameAndIdModels { get; 放; } 列出 listSources值:
var viewModel = new ArticleCreateViewModel()
{
SourceNameAndIdModels = listSources
};
您可以在我上面發布的控制器代碼中看到這一點。接下來,我將viewModel發送到View。
我的觀點代碼:
@model ArticleCreateViewModel
<div class="container">
<h2>Edit article</h2>
<div class="row gx-5">
<form asp-controller="Article" asp-action="Create" asp-antiforgery="true" method="post">
<div>
<input type="hidden" asp-for="Id" />
<div class="mb-3">
<label class="col-sm-2 col-form-label" asp-for="Title"></label>
<input class="form-control" type="text" asp-for="Title">
</div>
<div class="mb-3">
<label class="col-sm-2 col-form-label" asp-for="Description"></label>
<textarea class="form-control" asp-for="Description"></textarea>
</div>
<div class="mb-3">
<label class="col-sm-2 col-form-label" asp-for="Body"></label>
<textarea class="form-control" asp-for="Body"></textarea>
</div>
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Source
</a>
<ul class="dropdown-menu">
@foreach (var item in @Model.SourceNameAndIdModels)
{
<li><select class="dropdown-item" asp-for="SourceId" asp-items="@item.Id">@item.Name</select></li>
}
</ul>
</div>
<div class="mb-3">
<label class="col-sm-2 col-form-label" asp-for="CreationDate"></label>
<input type="datetime-local" class="form-control" asp-for="CreationDate">
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
最后,在代碼的這個地方,我想設定我正在創建的文章的來源,我有一個錯誤:
在此處輸入影像描述
您能告訴我如何在我的請求中通過下拉選單與我的代碼交朋友嗎?我究竟做錯了什么?
uj5u.com熱心網友回復:
以這種方式使用asp-items是不正確的。
Select Tag Helper asp-items指定選項元素
詳細資訊和示例:
https ://learn.microsoft.com/...netcore-6.0#the-select-tag-helper
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/533076.html
