Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a View with 2 panels which is SignIn and SignUp .In signIn Panel i have Login button and Clear button ,In signUp Panel I have SignUp and Clear Button.
I got this error when i try to login with wrong password.

Model.
C#
public ActionResult Index()
        {

            var list = new SelectList(new[]
{
    new { ID = "1", Name = "MySelf" },
    new { ID = "2", Name = "My Son" },
    new { ID = "3", Name = "My Daughter" },
    new{ ID="4" ,Name="My Brother"},
     new { ID = "5", Name = "My Sister" },
      new { ID = "6", Name = "My Relative" },
      new { ID = "6", Name = "My Friend" },
},
"ID", "Name",1);


            ViewBag.profileList = list;

            ViewBag.ReligionList = new SelectList(db.Religion, "ReligionId", "ReligionName");

            ViewBag.languageList = new SelectList(db.Languages, "LanguageId", "LanguageName");
            ViewBag.StateList = new SelectList(db.States, "stateId", "Statename");
            return View();
        }
 [HttpPost]
        public ActionResult Index(MemberLogin memberlogin)
        {
           

                if (ModelState.IsValid)
                {

                    bool value = db.MemberLogins.Any(o => o.EmailAddress == memberlogin.EmailAddress &&
                        o.Password == memberlogin.Password);

                    if (value)
                    {
                        return RedirectToAction("Index", "MemberHome");
                    }
                    else
                    {
                        return View();
                    }
                }
               
           

            return View();
        }

My question is

1) [HttpPost] method get fires when i click any button in my View, How do i know which button is Clicked?if i clicked signin button i got to move Home page if clear clicked i got to clear all controls.

2) I got this error

C#
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'profileList'

when i click SignIn Button or SignUp button but the ProfileList Loaded completely before i Click any button.
How do i solve this?
Posted
Updated 30-Mar-15 23:46pm
v3

1 solution

1.The problem is generated by the fact that in your view you are expecting to have for the key profileList an enumerable/list of SelectListItem , but you do not build such list in your Index method.

2.Your variable list from your Index method is an object of type SelectList.

3.Better is to use a Model for your view and not to pass data by using ViewBag. So you have to create a class that will have 4 properties, one property for each list. Then to create an object of this class and populate each property with their items.
 
Share this answer
 
Comments
King Fisher 30-Mar-15 14:02pm    
Not Clear sir,I'm new to MVC.
Raul Iloc 31-Mar-15 7:32am    
MVC come from Model, View, Controller. So in the communication between Controller and View a Model should be used to stores all the needed data. So in your case, the Model class have to contains all the list that you want to pass from the controller to the view.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900