tags:

views:

59

answers:

1

Hello

I'm trying to "convert" this into a strongly typed model instead of ViewData. But how should that look?

I got this in my controller:

    public ActionResult UserEdit(int? userID)
    {
        User u;

        if (userID == null)
        {
            u = new User();
        }
        else
        {
            u = _us.GetUsers(userID.Value).First();
            u.Usergroups.Load();
        }

        List<int> selectedUsergroupIDs = new List<int>();

        foreach (Usergroup item in u.Usergroups)
        {
            selectedUsergroupIDs.Add(item.UsergroupID);
        }

        MultiSelectList UsergroupID = new MultiSelectList(_ug.GetUsergroups(), "UsergroupID", "UsergroupName", selectedUsergroupIDs);

        ViewData["UsergroupID"] = UsergroupID;


        return View("UserEdit", new UserAdminEditViewModel { User = u, Usergroups = _ug.GetUsergroups() });
    }

"UsergroupID" in this case, what's needed to add in my model and in my view?

Thanks in advance

/M

+1  A: 

Not sure I understand the entire question, but if you're asking what can you do to make this model usable in the page, make sure the page inherits from ViewPage<UserAdminEditViewModel>.

Also, the MultiSelectList class has both the selected and unselected values so you don't need to have both of them in the view model so you can pair them down to a single member of the model. You'll need to change UserAdminEditViewModel to look something like this.

public class UserAdminEditViewModel
{
  public User User { get; set; }
  public IEnumerable<SelectListItem> UserGroups { get; set; }
}

At the end of your controller:

userGroups = new MultiSelectList(_ug.userGroups(), "Id", "Name", (from g in User.Usergroups select g.Id.Value).ToList()), new { size = 6 })
return View("UserEdit", new UserAdminEditViewModel { User = u, UserGroups = userGroups });

Then, in your view page:

<%@ Page Title="User Admin Edit" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UserAdminEditViewModel>" %>
<%-- ... --%>

    <% using (Html.BeginForm()) {%>

            <div>
                <label for="Title">User name:</label>
                <%= Html.TextBox("Title", Model.User.Username) %>
            </div>
            <div>
                <label for="Groups">Groups:</label>
                <%= Html.ListBox("SelectedGroups", Model.UserGroups)%>
            </div>
            <%-- ... --%>
    <% } %>
<%-- ... --%>

Also, note that you're invoking _ug.GetUsergroups() twice. That should go away with this solution but if you still need to reference it I'd recommend caching it within the action if you're not caching it in the method call itself so you don't hit the database every time you need the list.

I think this answers your question. If not, please clarify. Also, I haven't tested this code so it's not guaranteed to compile but it should work.

andymeadows
_ug.GetUsergroups() gets all usergroups, how do I apply the selected ones in "u.Usergroups" to that so it gets right type etc? More stuff in model?
molgan
It's part of the MultiSelectList. I fixed an error in what I provided bove as for returning the View and added some more code.
andymeadows
By part of the MultiSelectList, the list itself contains all values and then the values selected within the current object.
andymeadows
How shall the post-back look? I get that "There is no ViewData item with the key 'SelectedGroups' of type 'IEnumerable<SelectListItem>'."
molgan