Home > Net >  ASP.NET Core 6 - Model binding issue
ASP.NET Core 6 - Model binding issue

Time:10-30

I want to edit blogs but my ViewModel can't get CategoryId and BlogId.

enter image description here

This is the edit view:

enter image description here

I can get other data for example Title, Description but CategoryId returns 0.

This is BlogEditVM:

enter image description here

If you need more details I can edit my post, thanks.

CodePudding user response:

You are passing the CategoryId value as Category.CategoryId to API action. Hence, you cannot get the CategoryId value from the BlogEditVM object in the API action.

You need to specify the name attribute in the <select> element as below:

<select asp-items="@ViewBag.values"
    asp-for="@Model.Category.CategoryId"
    
    name="CategoryId">
    ...
</select>
  • Related