ASP.NET MVC获取文本框输入值

use*_*552 70 html c# asp.net-mvc razor asp.net-mvc-4

我有一个文本框输入和一些单选按钮.例如,我的文本框输入HTML看起来像这样:

<input type="text" name="IP" id="IP" />
Run Code Online (Sandbox Code Playgroud)

一旦用户点击网页上的按钮,我想将数据传递给我的控制器:

<input type="button" name="Add" value="@Resource.ButtonTitleAdd"  onclick="location.href='@Url.Action("Add", "Configure", new { ipValue =@[ValueOfTextBox], TypeId = 1 })'"/>
Run Code Online (Sandbox Code Playgroud)

也许这是微不足道的,但我的问题是我不知道如何获取文本框值并将其传递给控制器​​.如何读取文本框值并通过它传递给控制器ipValue=@[ValueOfTextBox]

And*_*rei 137

带有电子邮件文本框的简单ASP.NET MVC订阅表将实现如下:

模型

表单中的数据将映射到此模型

public class SubscribeModel
{
    [Required]
    public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

视图

视图名称应与控制器方法名称匹配.

@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>
}
Run Code Online (Sandbox Code Playgroud)

调节器

Controller负责请求处理并返回正确的响应视图.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的项目结构.请注意,"Home"视图文件夹与HomeController名称匹配.

ASP.NET MVC结构


And*_*bal 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>
Run Code Online (Sandbox Code Playgroud)


小智 14

试试这个.

视图:

@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" />
}
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public ActionResult Login(string IP, string Name)
{
    string s1=IP;//
    string s2=Name;//
}
Run Code Online (Sandbox Code Playgroud)

如果你可以使用模型类

[HttpPost]
public ActionResult Login(ModelClassName obj)
{
    string s1=obj.IP;//
    string s2=obj.Name;//
}
Run Code Online (Sandbox Code Playgroud)


And*_*ski 6

使用ajax方法的另一种方法:

视图:

@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>
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
public EmptyResult YourMethod(string txtValue)
{
    // do what you want with txtValue
    ...
}
Run Code Online (Sandbox Code Playgroud)