简体   繁体   中英

Not able to compare string in asp.net c#

Here is my logic in a code snippet.

I am trying to login, if data comes from web-page and if it matches with the database to proceed allowing to login

[HttpPost]//post method
public ActionResult Index(FormCollection collection)//using formcollection
{

    var logindata = amcs.Logins.Where(a => a.Admin_Email_Id == collection["Admin_Email_Id"] && a.Admin_Password == collection["Admin_Password"]).SingleOrDefault();//compare string
    if (logindata == null)//match data
    {             
        return Redirect("/Contract/Login/index");//redirect if not match
    }
    else
    {
        Session["Emailids"] = collection["EmailId"];//add session 
        Session["Passwords"] = collection["AdminPassword"];
        return Redirect("/Contract/Homepage/index");
    }
}

If you are getting NULL as a result, have you looked further into this yourself?

For example, what values are

  • collection["Admin_Email_Id"]
  • collection["Admin_Password"]

Does the data in amcs.Logins contain objects whose properties match those values? You can hover the mouse of the data and look at it in the debugger.

EDIT In response to a comment:

In the HTML does the

<input type="text" id="Admin_Email_Id"> also have an attribute name="Admin_Email_Id" ? If not then add it manually eg

Html.TextBoxFor(m => m.Admin_Email_Id, new { name = "Admin_Email_Id", PlaceHolder = "EmailId"})

I'd be surprised that you need to do that though, but it's worth checking the HTML to check that name is there. Without name , when posting to the controller, the FormColleciton won't have a value for that missing name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM