0

I can get all Roles plus actually Role for chosed user, but then When I posting to EditUser action, then Dropdownlist sends null. I mean When the form posts to my controller, I get null from DropDownList.

Here is my Model

public class EditUserViewModel
    {
      public string Id { get; set; }
      public string Name { get; set; }
      public string Email { get; set; }
      public List<SelectListItem> ApplicationRoles { get; set; }
      public string ApplicationRoleId { get; set; }
    }

Here is Action

[HttpGet]
            public async Task<ActionResult> EditUser(string id)
            {
                EditUserViewModel model = new EditUserViewModel();
                model.ApplicationRoles = RoleManager.Roles.Select(r => new SelectListItem
                {
                    Text = r.Name,
                    Value = r.Id
                }).ToList();


                if (!String.IsNullOrEmpty(id))
                {
                    ApplicationUser user = await UserManager.FindByIdAsync(id);
                    if (user != null)
                    {
                        var role = await UserManager.GetRolesAsync(user.Id);
                        var existingRole = role.First();
                        string existingRoleId = RoleManager.Roles.Single(r => r.Name == existingRole).Id;
                        model.Id = user.Id;
                        model.FirstName = user.FirstName;
                        model.ApplicationRoleId = existingRoleId;
                        ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name", model.ApplicationRoleId); 
                    }
                }
                return PartialView("_EditUser", model);
            }

And here is DropDownlist from _EditUser.cshtml

<div class="form-group">
                @Html.Label("Role typ", htmlAttributes: new { @class = "control-label col-md-6" })
                <div class="col-md-12" title="Ange antal datorer som finns i lager">
                    @Html.DropDownList("RoleId", null, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.ApplicationRoles, "", new { @class = "text-danger" })
                </div>
            </div>

Getting null Only from DropDownList, not from @Html.EditorFor /Thanks in advance!

4
  • 1
    Show you model and the signature of the POST method (does your model really contain a property named RoleId? - based on the code in your get method your view code will be @Html.DropDownListFor(m => m.ApplicationRoleId, Model.ApplicationRoles) Commented Nov 24, 2017 at 20:40
  • @StephenMuecke Thank you. Now it's working . But I don't know how to accept your answer Commented Nov 24, 2017 at 20:49
  • I have not given an answer (and will not until you edit your question as I asked) Commented Nov 24, 2017 at 20:50
  • @StephenMuecke Aha Ok, now I have edited with ViewModel ..;) Commented Nov 24, 2017 at 20:54

2 Answers 2

1

Forms post back the name/value pairs of their successful form controls. Your generating a <select> element with name="RoleId" but you model does not contain a property named RoleId. Since you want to bind the selected option to the ApplicationRoleId role property, then you view needs to be

@Html.LabelFor(m => m.ApplicationRoleId)
@Html.DropDownListFor(m => m.ApplicationRoleId, Model.ApplicationRoles)
@Html.ValidationMessageFor(m => m.ApplicationRoleId)

Notes:

  1. Your current @Html.Label(..) code does not create a label associated with your dropdownlist (clicking on it will not set focus)
  2. The ValidationMessageFor() need to be applied to the property your binding to, not the SelectList
  3. Delete you ViewBag.RoleId = new SelectList(..) code. Your have already assigned the selectlist to the ApplicationRoles property (and you should never need ViewBag if have a view model anyway)
Sign up to request clarification or add additional context in comments.

Comments

0

Because you are declare that only HttpGet methods are allow in that method of the controller. Thats why

1 Comment

Thank you guys all of you, it's working as @StephenMuecke responsed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.