简体   繁体   English

在 ASP.NET Core MVC Razor 视图中,与@model IEnumerable 相关的问题<namespace.model>对比@model<namespace.model></namespace.model></namespace.model>

[英]In an ASP.NET Core MVC Razor view, issue related to @model IEnumerable<Namespace.Model> Vs @model<Namespace.Model>

In an ASP.NET Core MVC Razor view, I have 2 forms. One input form is used to populate 2 dropdown lists as ajax modelpopup and saving values in database.在 ASP.NET Core MVC Razor 视图中,我有 2 个 forms。一个输入表单用于填充 2 个下拉列表作为 ajax modelpopup 并将值保存在数据库中。 Another form is used to display the related values as datatable in same view.另一种形式用于在同一视图中将相关值显示为数据表。

When I try to populate data.table by @foreach(var item in Model) loop by declaring @model IEnumerable<Namespace.Model> in view, it works fine for assigning value to datatable controls.当我尝试通过在视图中声明@model IEnumerable<Namespace.Model>来通过@foreach(var item in Model)循环填充 data.table 时,它可以很好地为数据表控件赋值。

But other form having dropdownlist shows error.但是其他有下拉列表的表格显示错误。 Not able to declare asp-for for select control.无法声明 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;
    }            
}

When I change from @model IEnumerable<Namespace.Model> to @model<Namespace.Model> , I am able to access dropdownlist in the form but @foreach(var item in Model) shows an error.当我从@model IEnumerable<Namespace.Model>更改为@model<Namespace.Model>时,我可以访问表单中的下拉列表,但@foreach(var item in Model)显示错误。 So I need to populate with viewbag.所以我需要填充 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>

It's not advisable to use viewbag and viewdata.不建议使用viewbag和viewdata。 Please guide me how to achieve this by using strongly typed view for the above requirement.请指导我如何通过针对上述要求使用强类型视图来实现这一点。

You can remove the asp-for tag helper in the select tag, and manually set its id and name可以去掉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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM