tags:

views:

45

answers:

1

Hello

I'm having problems with a Listbox, I'm going to add validation to a page where you add/edit a user, and when the page loads and I add a modelstate-error to force it to stop, it complains about my listbox doesnt contain any data with a key.

On the page I have:

 <p>
            <label for="Email">E-post: *</label><br />
            <%= Html.TextBox("Email", Model.User.Email)%>
            <%= Html.ValidationMessage("Email", "*") %>
        </p>
        <p>
            <label for="Password">Lösenord: *</label><br />
            <%= Html.TextBox("Password", Model.User.Password) %>
            <%= Html.ValidationMessage("Password", "*") %>
        </p>
        <p>
            Användargrupper: <br />
           <%= Html.ListBox("selUsergroups")%>
            <br />
        </p>

And in my controller I have:

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(), selUsergroups = new MultiSelectList(_ug.GetUsergroups(), "UsergroupID", "UsergroupName", selectedUsergroupIDs) });
    }


    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UserEdit([Bind(Include = "UserID,Username,Firstname,Surname,Email,Password")]User userobj, int[] UsergroupID, int[] selUsergroups)
    {

        ModelState.AddModelError("asdfasfd", "asfdasdfsfd");

        if (ModelState.IsValid)
        {
            try
            {
                _us.SaveUser(userobj);

                if (UsergroupID != null)
                    _us.SaveUserUsergroups(userobj.UserID, UsergroupID);

                return RedirectToAction("UserDetail", new { userID = userobj.UserID });

            }
            catch
            {
                ModelState.AddModelError("SomeError", "errrrrrrrrror");
            }

        }

        return View("UserEdit", new UserAdminEditViewModel { User = userobj });
    }

And my model looks like this:

    public class UserAdminEditViewModel
{
    public User User { get; set; }
    public IEnumerable<Usergroup> Usergroups { get; set; }
    public PaginatedList<User> Users { get; set; }
    public MultiSelectList selUsergroups { get; set; } 

    public UserAdminEditViewModel() {}
}

I've been trying with both "IEnumerable Usergroups" and "public MultiSelectList selUsergroups { get; set; }" but still it complains that there is no data with key blah that has IEnumerable

Whats needed to do? I would prefer to use some strongly typed way.

Thanks in advance

/M

A: 

You might find this link of some use What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?

Also, I think if you are going to return to the same page you're going to want to fill the model with data again.

As an alternative have you considered a jQuery post so that you don't have to fill the model again?

griegs
yep i've been looking on that for weeks now, and can't get mine to work
molgan