1

I have been browsing stack for an answer and I see so many different ways of doing this, none of them works when i try them though. so maybe I am doing something fundamentally wrong in my code.

I have a dropdownlistfor which looks like this:

@Html.DropDownListFor(m => m.PrivilegesGroups.First().Id, new SelectList(Model.PrivilegesGroups, "Id", "Name"), new { id = "PrivilegeGroupIdDDL", @class = "tableInput" })

This is my model.

namespace My.Internal.Models {
  public class UserViewModel {
    public User User { get; set; }
    public IEnumerable<PrivilegeGroup> PrivilegesGroups { get; set; }
    public IEnumerable<Company> UserCompanies { get; set; }
  }
}

Where User is a class with an Id, Name, UsersCompanyId and PrivilegeGroupId.

PrivilegeGroup is a class with: Id,Name.

Company is a class with: Id,Name.

My controller for saving looks like this.

    [HttpPost]
    public IActionResult ManageUsers(UserViewModel model) {
        UserDao db = new UserDao();
        User modelToSave = new User();
        db.AddUpdateUser(modelToSave);
        return RedirectToAction("ManageUsers");          
    }

I have tried different ways of getting the value of the selected dropdownlist item but have yet to manage to have it come back in the UserViewModel.

Any help in how to get the ID of the selected item would be much appreciated.

Rekshino
  • 6,954
  • 2
  • 19
  • 44
MichaelA
  • 57
  • 1
  • 11
  • possible duplicate of [How to get DropDownList SelectedValue in Controller in MVC4](http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4) – Ehsan Sajjad Jan 29 '15 at 09:10
  • this post will help: http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4 – Ehsan Sajjad Jan 29 '15 at 09:11

1 Answers1

0

You're doing two main things wrong here. First, you cannot execute a function in an expression like this:

 m => m.PrivilegesGroups.First().Id

You will have to do the execution before you call the expression. Either add a variable to your ViewModel to contain the result of this, or execute the method into a local variable in your view (not really recommended, but it would work)

The other problem is that you're overriding the id:

new { id = "PrivilegeGroupIdDDL", @class = "tableInput" })

This is not good because you are taking away the helpers ability to prevent ID conflicts. This can result in invalid HTML and unexpected results.

As far as coming back in the UserViewModel, are you using an Html.BeginForm() correctly?

EDIT:

To add the ID to your model, just do something like this:

public class UserViewModel {
    public User User { get; set; }
    public int PrivelegesId { get; set; }
    public IEnumerable<PrivilegeGroup> PrivilegesGroups { get; set; }
    public IEnumerable<Company> UserCompanies { get; set; }
}

And in your controller you, execute the query:

model.PrivelegesId = myQuery.PrivelegesGroup.First().Id;

Then in your DropDownList:

DropDownListFor(m => m.PrivilegesId, 
   new SelectList(Model.PrivilegesGroups, "Id", "Name"), new { @class = "tableInput" })

I don't know what your Controller code looks like, so I just guessed at variable names, but you should be able to figure it out.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Hi Erik, sorry for my late answer i was stuck with illness. I removed the ID i gave it and tried my best to figure out what way i tought best as far as the execustion of the function in the expression. However i have yet to figure out exactly what would work the best. You suggested that i should try and make a variable to hold it inside the Model, could you perhaps show an example of this approch? even if pseudo its just so im 100% with you :) Thanks in advance. – MichaelA Feb 02 '15 at 07:10
  • i forgot to say am using the Html.BeginForm, the model is returned to me, it simply has null values where the dropdown values go. – MichaelA Feb 02 '15 at 07:18