我有一个文本框输入和一些单选按钮。例如,我的文本框输入HTML如下所示:
<input type="text" name="IP" id="IP" />
一旦用户点击了网页上的一个按钮,我就想把数据传递给我的控制器:
<input type="button" name="Add" value="@Resource.ButtonTitleAdd" onclick="location.href='@Url.Action("Add", "Configure", new { ipValue =@[ValueOfTextBox], TypeId = 1 })'"/>
也许它是微不足道的,但我的问题是,我不知道如何获取textbox值并将其传递给控制器。如何通过ipValue=@[ValueOfTextBox]
读取TextBox值并将其传递给控制器
发布于 2013-09-18 21:14:05
带有电子邮件文本框的简单ASP.NET MVC订阅表单将像这样实现:
模型
表单中的数据映射到此模型
public class SubscribeModel
{
[Required]
public string Email { get; set; }
}
视图
视图名称应与控制器方法名称匹配。
@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>
}
控制器
控制器负责请求处理和返回正确的响应视图。
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);
}
}
这是我的项目结构。请注意,“主页”视图文件夹与HomeController名称匹配。
发布于 2013-09-18 21:23:22
您可以使用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>
发布于 2018-02-03 01:49:18
你可以做的很简单:
首先:例如,在模型中,您可以使用此实现的User.cs
public class User
{
public string username { get; set; }
public string age { get; set; }
}
我们将空模型传递给用户-当用户像这样提交表单时,这个模型将填充用户的数据
public ActionResult Add()
{
var model = new User();
return View(model);
}
当您返回View by empty User作为模型时,它将映射到您实现的表单的结构。我们在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>
}
所以在提交按钮上,你可以像这样使用它
[HttpPost]
public ActionResult Add(User user)
{
// now user.username has the value that user entered on form
}
https://stackoverflow.com/questions/18873098
复制相似问题