![](/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.