ASP.NET MVC获得文本框输入值 [英] ASP.NET MVC get textbox input value
问题描述
我有一个文本框输入一些单选按钮。例如我的文本框输入HTML看起来像:
I have a textbox input and some radio buttons. For example my textbox input HTML looks like that:
<input type="text" name="IP" id="IP" />
一旦用户点击一个网页,我想将数据传递给我的控制器上的按钮:
Once user clicks a button on a web page I want to pass data to my controller:
<input type="button" name="Add" value="@Resource.ButtonTitleAdd" onclick="location.href='@Url.Action("Add", "Configure", new { ipValue =@[ValueOfTextBox], TypeId = 1 })'"/>
也许是微不足道的,但我的问题是,我不知道如何获取文本框的值,并通过它传递给控制器。我怎样才能读取文本框的值,并通过 ipValue = @ [ValueOfTextBox]
?
推荐答案
将在&LT所有的投入;形式...&GT;
标记。您可以使用HTML标记或ASP.NET MVC帮手 @ Html.BeginForm(...)
。当您提交表单,所有输入数据将被发送到一个控制器动作。请看一个例子:
Put all your inputs in a <form ...>
tag. You can use HTML tag or ASP.NET MVC helper @Html.BeginForm(...)
. When you submit your form, all the input data will be sent to a controller action. Please see an example:
这形式的数据将被解析到该模型,并传递给所述控制器。
The data from the form will be parsed to this model and passed to the controller.
public class UserLogin
{
public string Email { get; set; }
public string Password { get; set; }
}
查看(剃刀)
文件位置是非常重要的。您的文件夹名称应与控制器名和文件名应该是相同的控制器动作:帐户\\ Login.cshtml
@model UserLogin
@using (Html.BeginForm("Login", "Accounts", FormMethod.Post))
{
@Html.TextBoxFor(m => m.Email)
@Html.PasswordFor(m => m.Password)
<!--
<input type="password" name="Password" />
You can use HTML inputs, but it's more reliable to use strongly-typed MVC helpers.
-->
<input type="submit" value="Login" />
}
控制器
控制器是负责数据处理。这也决定什么视图来显示给用户。
Controller
Controller is responsible for data processing. It also decides what view to show to the user.
public class AccountsController: Controller
{
public ActionResult Login()
{
// shows login page.
return View();
}
public ActionResult Home()
{
// shows home page. Make sure you have Accounts\Home.cshtml view.
return View();
}
[HttpPost]
public ActionResult Login(UserLogin model)
{
// if credentials are correct.
if (accountsService.Login(model))
{
// redirect to home page.
return View("Home");
}
else
{
// show login page again.
return View();
}
}
}
这篇关于ASP.NET MVC获得文本框输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!