1

I am starting up with mvc and designing a simple login procedure. I have view with login screen with two inputs username and password. but apparently not able to get how can i pass the values of inputs from my view to controller i am using razor. here are my snippets.

<table>
    <tr>
        <td>
            UserName:
        </td>
        <td>
            @Html.TextBox("userName")
        </td>
    </tr>
        <tr>
        <td>
            Password
        </td>
        <td>
            @Html.Password("Password")
        </td>
    </tr>
    <tr>
        <td colspan="2">
            @Html.ActionLink("login", "SignIn")
        </td>
    </tr>
</table>

and my controller looks like this.( i am able to redirect to controller using action link just fine.just about passing values.)

public ActionResult SignIn()
{
    //string userName = Request["userName"];
    return View("Home");
}

3 Answers 3

2

You can enclosed above html stuff inside form container where you declared form submission method as POST.

@using (Html.BeginForm("SignIn", "Controller", FormMethod.Post, new { id = "form1" }))
{
    <table>
    <tr>
        <td>
            UserName:
        </td>
        <td>
            @Html.TextBox("userName")
        </td>
    </tr>
        <tr>
        <td>
            Password
        </td>
        <td>
            @Html.Password("Password")
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="login" name="login" />
        </td>
    </tr>
</table>
}

Then, you can put Post Action in your controller:

[HttpPost]
public ActionResult SignIn(FormCollection frmc)
{
    /// Extracting the value from FormCollection
    string name = frmc["userName"];
    string pwd = frmc["Password"];
    return View("Home");
}
Sign up to request clarification or add additional context in comments.

2 Comments

perfect! that brings me to question.is that the best practice? or is there something better?
I would make the view with an @model VmLoginParams and make the controller post action take public ActionResult SignIn(VmLoginParams params) In the Html code you can then do @Html.EditorFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName) and in the controller check for ModelState.IsValid: if (!ModelState.IsValid) return View(params) (See also: mikesdotnetting.com/Article/188/…)
0

Wrap your table in Form:

    @using (Html.BeginForm("SignIn", "controllerName", FormMethod.POST))
{
<table>
...
</table>
<input type="submit" value="Sign in" />
}

And in controller write:

[HttpPost]
public ActionResult SignIn(string userName, string Password)
{
 //sign in and redirect to home page
}

Comments

0

View:

@using (Html.BeginForm("SignIn", "Controller", FormMethod.Post, new { id = "form1" }))
{
<table>
<tr>
    <td>
        UserName:
    </td>
    <td>
        @Html.TextBox("userName")
    </td>
</tr>
    <tr>
    <td>
        Password
    </td>
    <td>
        @Html.Password("Password")
    </td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="login" name="login" />
    </td>
</tr>
</table>
}

Model:

public string userName{get;set;}
public string Password{get;set;}

Controller:

[HttpPost]
 public ActionResult SignIn(Model obj)
 {
  //sign in and redirect to home page
   string userName = obj.username;
   string password = obj.password;
 }

it may be help full to you.

Comments

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.