0

i want to perform a database action on login click. So i am using stored procedure for getting the data, but here i am facing some issues regarding model exception.

public ActionResult Login(UserInfo model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                List<UserInfo> loginDetails = dbContext.Database.SqlQuery<UserInfo>
                    ("exec spGetloginUserInfo @username,@password", new SqlParameter("@username", model.username), new SqlParameter("@password", model.password)).ToList();
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

My Model

  public class UserInfo
    {
        public string usertype { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public bool active { get; set; }
        public DateTime lastmodifieddate { get; set; }
        public string modifiedby { get; set; }
        public bool RememberMe { get; set; }
    }

The exception thrown is

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Please let me know where i am doing wrong and also whether my approach is right? And also is there any other way for database interaction other that stored procedure approach.

  • are you sure all not nullable properties are passed before calling your Login action, i.e. lastmodifieddate ? regarding your approach, if I were u, I would write my logic in linq instead of SP to avoid scattering my business logic in more than one tier – Muhammad Gouda Dec 27 '14 at 14:07
  • ya i am sure all are not nullable. how do i use linq in this case. can you please throw more light on this. – Aniruddha K Purohit Dec 27 '14 at 14:16
  • Its asking me to define a key for EntityType 'UserInfo'. I dont know what kind of key it is looking for – Aniruddha K Purohit Dec 27 '14 at 15:52
  • I didn't mean make all properties not nullable, because this depends on your business. I mean if a value is not nullable you should pass it a value to make the ModelStat.IsValid equal true. For using Linq, I need to know the nature of data you want to retrieve to be able to help. but I recommend you read some tutorial or search from online linq sample codes – Muhammad Gouda Dec 27 '14 at 17:31

1 Answers1

0

ModelState.AddModelError throws an error but your client/javascript/views did not catch that error.

Here is a link on how to implement similar error handling in views.

ASP.NET MVC 2 Model Errors with Custom Exceptions

Also, make sure the your model UserInfo has valid data for each properties

Login(UserInfo model, string returnUrl) <<-- the model of type UserInfo
Community
  • 1
  • 1
agentpx
  • 1,051
  • 1
  • 8
  • 24