MVC View to controller Passing

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");
}

Answers


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");
}

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
}

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.


Need Your Help

What password encryption Jenkins is using?

java jenkins hudson

I am modifying an xml of a Jenkins job. There is a field which is a password. When I get the xml, where it was the raw password now there is a hash.

simplify settings for deb only build with sbt native packager

scala sbt dpkg sbt-native-packager

I am trying to package my scala main app code as a .deb file. the app will only run on an ubuntu machine, so I do not really care about windows etc.