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]
将其传递给控制器?
Maybe it is trivial but my problem is that I do not know how to get textbox value and pass it through to the controller. How can I read the textbox value and pass it to the controller through ipValue=@[ValueOfTextBox]
?
推荐答案
带有电子邮件文本框的简单 ASP.NET MVC 订阅表单将如下实现:
Simple ASP.NET MVC subscription form with email textbox would be implemented like that:
表单中的数据映射到这个模型
The data from the form is mapped to this model
public class SubscribeModel
{
[Required]
public string Email { get; set; }
}
查看
视图名称应与控制器方法名称相匹配.
View
View name should match controller method name.
@model App.Models.SubscribeModel
@using (Html.BeginForm("Subscribe", "Home", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<button type="submit">Subscribe</button>
}
控制器
Controller 负责处理请求并返回正确的响应视图.
Controller
Controller is responsible for request processing and returning proper response view.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Subscribe(SubscribeModel model)
{
if (ModelState.IsValid)
{
//TODO: SubscribeUser(model.Email);
}
return View("Index", model);
}
}
这是我的项目结构.请注意,Home"视图文件夹与 HomeController 名称匹配.
Here is my project structure. Please notice, "Home" views folder matches HomeController name.
这篇关于ASP.NET MVC 获取文本框输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!