C# ASP.NET MVC 获取文本框输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18873098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC get textbox input value
提问by Ralph
I have a textbox input and some radio buttons. For example my textbox input HTML looks like that:
我有一个文本框输入和一些单选按钮。例如,我的文本框输入 HTML 如下所示:
<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 })'"/>
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]
?
也许这很简单,但我的问题是我不知道如何获取文本框值并将其传递给控制器。如何读取文本框值并将其传递给控制器ipValue=@[ValueOfTextBox]
?
采纳答案by Andrei
Simple ASP.NET MVC subscription form with email textbox would be implemented like that:
带有电子邮件文本框的简单 ASP.NET MVC 订阅表单将如下实现:
Model
模型
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 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);
}
}
Here is my project structure. Please notice, "Home" views folder matches HomeController name.
这是我的项目结构。请注意,“Home”视图文件夹匹配 HomeController 名称。
回答by Andriy Gubal
You may use jQuery:
你可以使用 jQuery:
<input type="text" name="IP" id="IP" value=""/>
@Html.ActionLink(@Resource.ButtonTitleAdd, "Add", "Configure", new { ipValue ="xxx", TypeId = "1" }, new {@class = "link"})
<script>
$(function () {
$('.link').click(function () {
var ipvalue = $("#IP").val();
this.href = this.href.replace("xxx", ipvalue);
});
});
</script>
回答by Andrei Karcheuski
Another way by using ajax method:
另一种使用ajax方法的方法:
View:
看法:
@Html.TextBox("txtValue", null, new { placeholder = "Input value" })
<input type="button" value="Start" id="btnStart" />
<script>
$(function () {
$('#btnStart').unbind('click');
$('#btnStart').on('click', function () {
$.ajax({
url: "/yourControllerName/yourMethod",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
txtValue: $("#txtValue").val()
}),
async: false
});
});
});
</script>
Controller:
控制器:
[HttpPost]
public EmptyResult YourMethod(string txtValue)
{
// do what you want with txtValue
...
}
回答by Vidhyadhar Galande
Try This.
尝试这个。
View:
看法:
@using (Html.BeginForm("Login", "Accounts", FormMethod.Post))
{
<input type="text" name="IP" id="IP" />
<input type="text" name="Name" id="Name" />
<input type="submit" value="Login" />
}
Controller:
控制器:
[HttpPost]
public ActionResult Login(string IP, string Name)
{
string s1=IP;//
string s2=Name;//
}
If you can use model class
如果你可以使用模型类
[HttpPost]
public ActionResult Login(ModelClassName obj)
{
string s1=obj.IP;//
string s2=obj.Name;//
}
回答by Ali Tabandeh
you can do it so simple:
你可以做到如此简单:
First: For Example in Models you have User.cs with this implementation
第一:例如在模型中,您有带有此实现的 User.cs
public class User
{
public string username { get; set; }
public string age { get; set; }
}
We are passing the empty model to user – This model would be filled with user's data when he submits the form like this
我们将空模型传递给用户——当他像这样提交表单时,这个模型将填充用户的数据
public ActionResult Add()
{
var model = new User();
return View(model);
}
When you return the View by empty User as model, it maps with the structure of the form that you implemented. We have this on HTML side:
当您将空用户的视图作为模型返回时,它会映射到您实现的表单的结构。我们在 HTML 端有这个:
@model MyApp.Models.Student
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.username, htmlAttributes: new {
@class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.username, new {
htmlAttributes = new { @class = "form-
control" } })
@Html.ValidationMessageFor(model => model.userame, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.age, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"
/>
</div>
</div>
</div>
}
So on button submit you will use it like this
所以在按钮提交上你会像这样使用它
[HttpPost]
public ActionResult Add(User user)
{
// now user.username has the value that user entered on form
}