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. 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.
But other form having dropdownlist shows error. Not able to declare asp-for for select control.
<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. So I need to populate with 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. 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-items="Jsonlist" id="ClientCatalog" name="ClientCatalog" class="form-control"></select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.