[英]MVC View to controller Passing
I am starting up with mvc and designing a simple login procedure. 我正在启动mvc并设计一个简单的登录程序。 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
. 您可以在表单容器中包含上面的html内容,您将表单提交方法声明为
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: 然后,您可以将
Post
Action放入控制器:
[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. 它可能会对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.