Cannot return view model after POST

Posted by Interfector on Stack Overflow See other posts from Stack Overflow or by Interfector
Published on 2010-04-30T21:33:27Z Indexed on 2010/04/30 21:37 UTC
Read the original article Hit count: 423

Filed under:
|

HI,

I am trying to return a model after a POST but I receive the following error:

Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object.

Line 29: public UsersModel getUser(Int32 id) {

Line 30: return (

Line 31: from users in db.NUsers

Line 32: join userSettings in db.NUsersSettings

The code in the controller looks like this:

    [AcceptVerbs( HttpVerbs.Post )]
    public ActionResult Edit( NUsers user ) {
        //Other code that doesn't affect the error :)
        UsersModel userDetails = this.usersObj.getUser( user.UserID );
        return View( userDetails );
    }

The getUser method looks like this:

    public UsersModel getUser(Int32 id) {
        return (
            from users in db.NUsers
            join userSettings in db.NUsersSettings
                on users.UserID equals userSettings.UserID
            where users.UserID == id
            select new UsersModel {
                UserId = Convert.ToInt32( users.UserID ),
                Email = Convert.ToString( users.Email ),
                FirstName = Convert.ToString( users.FirstName ),
                LastName = Convert.ToString( users.LastName ),
                Language = Convert.ToString( userSettings.Language ),
                Phone = Convert.ToString( users.Phone )
            } ).First();
    }

and finally the UsersModel if you think it is relevant:

public class UsersModel {
    public int      UserId;
    public string   Email;
    public string   FirstName;
    public string   LastName;
    public string   Language;
    public string   Phone;
    public List<NLanguages> Languages;

    public UsersModel() {
        Models.Languages languagesObj = new Models.Languages();
        Languages = languagesObj.getLanguages();
    }
}

Any thoughts on what might be causing the error and what can I do to fix it?

Thx

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about LINQ