![](/img/trans.png)
[英]Asp.net mvc view with Ienumerable model returns null on submit
[英]In an ASP.NET Core MVC Razor view, issue related to @model IEnumerable<Namespace.Model> Vs @model<Namespace.Model>
在 ASP.NET Core MVC Razor 視圖中,我有 2 個 forms。一個輸入表單用於填充 2 個下拉列表作為 ajax modelpopup 並將值保存在數據庫中。 另一種形式用於在同一視圖中將相關值顯示為數據表。
當我嘗試通過在視圖中聲明@model IEnumerable<Namespace.Model>
來通過@foreach(var item in Model)
循環填充 data.table 時,它可以很好地為數據表控件賦值。
但是其他有下拉列表的表格顯示錯誤。 無法聲明 select 控件的請求。
<div class="form-group"> @{var Jsonlist = new electList((System.Collections.IEnumerable)ViewData["JsonPropVD"], "Value", "Key");}
<select asp-items="Jsonlist" asp-for="ClientCatalog" class="form-control"></select>
</div>
public void GetJsonProperties()
{
PropertyInfo[] propertyInfosSM = typeof(SMJsonModel).GetProperties();
if (propertyInfosSM != null)
{
Dictionary<string, object> Jsondictresults = new Dictionary<string, object>();
foreach (PropertyInfo propertyInfoJson in propertyInfosSM)
{
Jsondictresults.Add(propertyInfoJson.Name, propertyInfoJson.Name);
}
ViewBag.VBJsonProp = Jsondictresults.Keys;
ViewData["JsonPropVD"] = Jsondictresults;
}
}
當我從@model IEnumerable<Namespace.Model>
更改為@model<Namespace.Model>
時,我可以訪問表單中的下拉列表,但@foreach(var item in Model)
顯示錯誤。 所以我需要填充 viewbag。
<tbody>
@foreach (K360Master Mappeddata in ViewBag.K360MappedData)
{
<tr>
<td>@Mappeddata.Id</td>
<td>@Mappeddata.ClientCatalog</td>
<td>@Mappeddata.K360catalog</td>
<td>
<button id="btnDelete" asp-action="DeletePost" asp-route-Id="@Mappeddata.Id" class="btn btn-danger mr-3">Delete</button>
</td>
</tr>
}
</tbody>
不建議使用viewbag和viewdata。 請指導我如何通過針對上述要求使用強類型視圖來實現這一點。
可以去掉select標簽中的asp-for
標簽助手,手動設置它的id和name
<select asp-items="Jsonlist" id="ClientCatalog" name="ClientCatalog" class="form-control"></select>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.