PeterInvincible
PeterInvincible

Reputation: 2310

MVC 4 App - Get data from database

First of all I'd like to say, that I am super new to the MVC pattern, sorry if I am asking a stupid question.

My problem: I am having trouble with the building of a profile page for my users. If a user goes to that page it's going to list information about them, like e-mail address, phone number, full name, etc..

Note: I am using the "Basic" project template with SimpleMemberShipProvider hadling user actions.

The problem comes with the database querying, to get the necessary data about the user.

Here's my UserProfile table data:

CREATE TABLE [dbo].[UserProfile] (
    [UserId]               INT            IDENTITY (1, 1) NOT NULL,
    [UserName]             NVARCHAR (MAX) NULL,
    [FirstName]            NVARCHAR (MAX) NULL,
    [LastName]             NVARCHAR (MAX) NULL,
    [Age]                  INT            NULL,
    [Sex]                  NVARCHAR (MAX) NULL,
    [SecretQuestion]       NVARCHAR (MAX) NULL,
    [SecretQuestionAnswer] NVARCHAR (MAX) NULL,
    [MoneyIn]              INT            NULL,
    [MoneyOut]             INT            NULL,
    [TimesWon]             INT            NULL,
    [Email]                NVARCHAR (MAX) DEFAULT ('') NOT NULL,
    [PhoneNumber]          NVARCHAR (MAX) NULL,
    [Address]              NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_dbo.UserProfile] PRIMARY KEY CLUSTERED ([UserId] ASC)
);

My 'User' model:

[Table("UserProfile")]
    public class User
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        [Column("UserName")]
        public string UserName { get; set; }
        [Column("Email")]
        [Required]
        public string Email { get; set; }
        [Column("FirstName")]
        public string FirstName { get; set; }
        [Column("LastName")]
        public string LastName { get; set; }
        [Column("PhoneNumber")]
        public string PhoneNumber { get; set; }
        [Column("Address")]
        public string Address { get; set; }
        [Column("Age")]
        [Required]
        public int Age { get; set; }
        [Column("Sex")]
        public string Sex { get; set; }
        [Column("SecretQuestion")]
        [Required]
        public string SecretQuestion { get; set; }
        [Column("SecretQuestionAnswer")]
        [Required]
        public string SecretQuestionAnswer { get; set; }
        [Column("MoneyIn")]
        public int MoneyIn { get; set; }
        [Column("MoneyOut")]
        public int MoneyOut { get; set; }
        [Column("TimesWon")]
        public int TimesWon { get; set; }
    } 

Here's my DbContext class:

public DbSet<User> Users { get; set; }

My controller with the 'Profile' action:

        [Authorize]
        public ActionResult Profil()
        {
            var model = db.Users.ToList();
            return View(model);
        }

And finally some relevant parts of my view to display the data:

@model IEnumerable<OneMillion.Models.User>

@foreach (var item in Model)
{
    @item.FirstName 
}

The error I get when trying to access the page as a logged in user:

Server Error in '/' Application.

The 'MoneyIn' property on 'User' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'.

Thanks!

Upvotes: 1

Views: 3281

Answers (2)

user32826
user32826

Reputation:

Your model and database definitions don't match, you have nullable fields in the database definition (Age, MoneyIn, MoneyOut and TimesWon) but non-nullable fields in the model.

Upvotes: 1

proggrock
proggrock

Reputation: 3289

try to set the properties to be a nullable type ->

    [Column("MoneyIn")]
    public int? MoneyIn { get; set; }
    [Column("MoneyOut")]
    public int? MoneyOut { get; set; }

Upvotes: 3

Related Questions