samedi 28 décembre 2019

can't bind model on web api post

I can't bind list of string to my model.

My model:

    public class EditUserForm
    {
        [BindProperty]
        public string Id { get; set; }
        [BindProperty]
        public string Username { get; set; }
        [BindProperty]
        public string Email { get; set; }

        public List<string> ActualUserRoles { get; set; }

        public EditUserForm(IdentityUser user)
        {
            Id = user.Id;
            Username = user.UserName;
            Email = user.Email;
        }
        public EditUserForm()
        {

        }

    }

In view I have connected with ActualUserRoles field html code as below:

<div>
   <label for="user-actual-roles" class="control-label">Actual user roles:</label>
   <select asp-for="ActualUserRoles" multiple class="form-control text-center h-75" id="user-actual-roles">
   </select>
</div>

From jQuery AJAX I add dynamically (after page load) options to make it async.

But after I send form with it to controller like below:

        [HttpPost]
        [Route("Edit/{userId}")]
        public async Task<IActionResult> Edit(string userId, EditUserForm editUserForm)
        {
            //here editUserForm.ActualUserRoles is null, but anoter field like Email has proper value
            return View("some-view.cshtml", editUserForm);
        }

I'm using ASP.NET Core 2.2.7

Please help




Aucun commentaire:

Enregistrer un commentaire