0

How do you check the value that has been entered in an ASP.NET MVC @Html.TextBox and compare it with a value in the database? I want to make an easy login, and want to see if the value that has been entered in the textbox is the same as that in the database

<tr><td>Username</td><td>:</td><td>@Html.TextBox("username", new { @value = ViewBag.username })</td></tr>

I tried things like creating a viewbag and then taking it to the controller, but it didnt seem to work.

5
  • Did you try anything ? Have you searched for it ? Commented Apr 1, 2013 at 14:47
  • I tried various kinds of things, and yes, I've searched for it, but without luck, maybe my searching abilities suck. Commented Apr 1, 2013 at 14:47
  • you could have posted the things that you have tried. i mean the code Commented Apr 1, 2013 at 14:52
  • I can hardly post anything because I tried to make a class and link it to the textbox, but then I realized, how will it ever link? Commented Apr 1, 2013 at 14:53
  • 1
    A great place to start is asp.net/mvc. Commented Apr 1, 2013 at 15:03

2 Answers 2

3

Create a viewmodel(a simple class) for this specific UI

public class LoginViewModel
{
  [Required]
  public string UserName { set;get;}

  [Required]
  [DataType(DataType.Password)]
  public string Password { set;get;}
}

Now in your GET action, create an object of this class and send to your view.

public ActionResult Login()
{
  var vm=new LoginViewMode();
  return View(vm);
}

Now in our login view(Login.cshtml)which is strongly typed to our LoginViewModel, we will use the TextBoxFor html helper method to render the textboxes for our UserName and Password fields.

@model LoginViewModel
@using(Html.Beginform())
{
  UserName 
  @Html.TextBoxFor(x=>x.UserName);

  Password
  @Html.TextBoxFor(x=>x.Password)
  <input type="submit" />
}

This will render a form which has action attribute value set to /YourCotnroller/Login. now we need to have an HttpPost action method to handle the form posting

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
  if(ModelState.IsValid)
  {
    string uName=model.UserName;
    string pass=model.Password.

    //Now you can use the above variables to check it against your dbrecords.  
    // If the username & password matches, you can redirect the user to 
    //  another page using RedirecToAction method
    //  return RedirecToAction("UserDashboard")

  }
  return View(model);
}
public ActionResult UserDashboard()
{
  //make sure you check whether user is logged in or not
  // to deny direct access without login
  return View();
}
Sign up to request clarification or add additional context in comments.

8 Comments

Are you getting the error when you access your GET action method ?
Sorry, I was being dumb, I forgot it was an overload action, but what do you mean with "//Now you can use the above variables to check it against your dbrecords.", because I cannot go like entity.login.username in my database.
@user1841216: you got the username/password values user entered in the variables. now you need to talk to your DB layer to get data and compare it as needed.
Sorry I'm kind of new to MVC, how do I compare it to my DB layer?
@user1841216 That is not an MVC specific question. It is a generic C#/ADO.NET question. Google search will give you plenty of answers.
|
0

Try like this:

Define model property in your Model class:

public class Login{

        [Required]
        [Remote("IsUserNameAvaliable", "Home")]
        public string username{get;set;}

        [Required]
        public string password{get;set;}
}

The Remote attribute that is placed will find the Method/Action with IsUserNameAvaliable in the controller name Home.

The remote attribute servers for this purpose in MVC.

public JsonResult IsUserNameAvaliable(string username)
        {
            //Check if there are any matching records for the username name provided
            if (_dbEntity.Users.Any(c => c.UserName == username))
            {
                //If there are any matching records found

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                string userID = String.Format(CultureInfo.InvariantCulture,
                                "{0} is not available.", username);

                return Json(userID, JsonRequestBehavior.AllowGet);
            }

        }

Now in your view strongly type the textbox

@model Application.Models.Login

@Html.TextBoxFor(m=>m.username)
@Html.ValidationMessageFor(m=>m.username)

Donot forget to include jquery validation scripts.

 @Scripts.Render("~/bundles/jquery")
 @Scripts.Render("~/bundles/jqueryval")

2 Comments

Thanks, but I didn't mean it like that, I don't need a JSon result, I just want to check if the textbox value is the same as in the database, thanks though ;D
yes you can check and notify whether the user is avaliable or not

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.