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.

4
  • 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 Commented Dec 27, 2014 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. Commented Dec 27, 2014 at 14:16
  • Its asking me to define a key for EntityType 'UserInfo'. I dont know what kind of key it is looking for Commented Dec 27, 2014 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 Commented Dec 27, 2014 at 17:31

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Its asking me to define a key for EntityType 'UserInfo'. I dont know what kind of key it is looking for
You should put a @Model modelname in your views

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.