skip to Main Content

I am trying to post data from form to action method but looks like model binder is not binding to the entities and I am getting null values in the action method, your help is much appreciated
please find the code snippet below.

Action method

[HttpPost]
    public async Task<IActionResult> Update(EditRoleViewModel model)
    {
        if (ModelState.IsValid)
        {

            var role = await _Roleame.FindByIdAsync(model.Id);
            if (role == null)
            {
                return RedirectToAction("notfound");
            }
            role.Name = model.RoleName;
            var result = await _Roleame.UpdateAsync(role);
            if (result.Succeeded)
            {
                return RedirectToAction("getAllRoles", "Administrator");
            }
        }
        else {
            ModelState.AddModelError(string.Empty, "Error");
        }

        return View();

    }

Model

public class EditRoleViewModel
{
    public EditRoleViewModel()
    {
        names = new List<string>();
    }
    public string Id;
    [Required]
    public string RoleName;
    public List<string> names;
}

View

    @model FirstCoreApplication.Model.EditRoleViewModel
<script src="~/twitter-bootstrap/js/bootstrap.js"></script>
<link href="~/twitter-bootstrap/css/bootstrap.css" rel="stylesheet" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">


@{
    ViewBag.Title = "Edit Role";
}

<h1>Edit Role</h1>

<form method="post" class="mt-3" asp-controller="Administrator" asp-action="Update">
    <div class="form-group row">
        <label asp-for="Id" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="Id" disabled class="form-control">
        </div>
    </div>
    <div class="form-group row">
        <label asp-for="RoleName" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="RoleName" class="form-control">
            <span asp-validation-for="RoleName" class="text-danger"></span>
        </div>
    </div>

    <div asp-validation-summary="All" class="text-danger"></div>

    <div class="form-group row">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Update</button>
            <a asp-action="ListRoles" class="btn btn-primary">Cancel</a>
        </div>
    </div>
    <div class="card">
        <div class="card-header">
            <h3>Users in this role</h3>
        </div>
        <div class="card-body">
            @if (Model.names.Any())
            {
                foreach (var user in Model.names)
                {
                    <h5 class="card-title">@user</h5>
                }
            }
            else
            {
                <h5 class="card-title">None at the moment</h5>
            }
        </div>
        <div class="card-footer">
            <a href="#" class="btn btn-primary" style="width:auto">Add Users</a>
            <a href="#" class="btn btn-primary" style="width:auto">Remove Users</a>
        </div>
    </div>
</form>

enter code here

2

Answers


  1. Chosen as BEST ANSWER

    I've spent couple of hours looking for an error in my code and the reason for the binder not working in my case was using a model with public fields rather than public properties:

    public class EditRoleViewModel
    {
        public EditRoleViewModel()
        {
            names = new List<string>();
        }
    
    
        public string Id { get; set; }
    
        [Required]
        public string RoleName { get; set; }
    
        public List<string> names { get; set; }
    
    }
    

  2. The form data you post does not match the property of your model.You do not post the parameter names .

    public List names { get; set; }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search