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