关于c#:ASP.NET MVC获取文本框输入值

ASP.NET MVC get textbox input value

我有一个文本框输入和一些单选按钮。 例如,我的文本框输入HTML如下所示:

1
<input type="text" name="IP" id="IP" />

用户单击网页上的按钮后,我想将数据传递到控制器:

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

也许这很琐碎,但我的问题是我不知道如何获取文本框值并将其传递给控制器。 如何读取文本框值并将其通过ipValue=@[ValueOfTextBox]传递给控制器?


具有电子邮件文本框的简单ASP.NET MVC订阅表单将这样实现:

模型

表单中的数据映射到该模型

1
2
3
4
5
public class SubscribeModel
{
    [Required]
    public string Email { get; set; }
}

视图

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

1
2
3
4
5
6
7
8
@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>
}

控制者

控制器负责请求处理并返回正确的响应视图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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名称匹配。

ASP.NET MVC structure


您可以使用jQuery:

1
2
3
4
5
6
7
8
9
10
<input type="text" name="IP" id="IP" value=""/>
@Html.ActionLink(@Resource.ButtonTitleAdd,"Add","Configure", new { ipValue ="xxx", TypeId ="1" }, new {@class ="link"})


  $(function () {
    $('.link').click(function () {
      var ipvalue = $("#IP").val();
      this.href = this.href.replace("xxx", ipvalue);
    });
  });

尝试这个。

视图:

1
2
3
4
5
6
7
@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" />
}

控制器:

1
2
3
4
5
6
[HttpPost]
public ActionResult Login(string IP, string Name)
{
    string s1=IP;//
    string s2=Name;//
}

如果可以使用模型类

1
2
3
4
5
6
[HttpPost]
public ActionResult Login(ModelClassName obj)
{
    string s1=obj.IP;//
    string s2=obj.Name;//
}

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

视图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Html.TextBox("txtValue", null, new { placeholder ="Input value" })
<input type="button" value="Start" id="btnStart"  />


    $(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
            });
       });
   });

控制器:

1
2
3
4
5
6
[HttpPost]
public EmptyResult YourMethod(string txtValue)
{
    // do what you want with txtValue
    ...
}

您可以这么简单:

第一:例如,在Models中,您具有使用此实现的User.cs

1
2
3
4
5
public class User
 {
   public string username { get; set; }
   public string age { get; set; }
 }

我们正在将空模型传递给用户-当用户提交这样的表单时,该模型将被用户的数据填充

1
2
3
4
5
public ActionResult Add()
{
  var model = new User();
  return View(model);
}

当您将"按空用户查看"作为模型返回时,它会与您实现的表单的结构对应。我们在HTML方面有这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@model MyApp.Models.Student
@using (Html.BeginForm())
 {
    @Html.AntiForgeryToken()

   
        <h4>Student</h4>
        <hr />
        @Html.ValidationSummary(true,"", new { @class ="text-danger" })
       
            @Html.LabelFor(model => model.username, htmlAttributes: new {
                           @class ="control-label col-md-2" })
           
                 @Html.EditorFor(model => model.username, new {
                                 htmlAttributes = new { @class ="form-
                                 control"
} })
                 @Html.ValidationMessageFor(model => model.userame,"",
                                            new { @class ="text-danger" })
           
       

       
            @Html.LabelFor(model => model.age, htmlAttributes: new { @class
                           ="control-label col-md-2" })
           
                @Html.EditorFor(model => model.age, new { htmlAttributes =
                                new { @class ="form-control" } })
                @Html.ValidationMessageFor(model => model.age,"", new {
                                           @class ="text-danger" })
           
       
       
           
                <input type="submit" value="Create" class="btn btn-default"
                 />
           
       
   
}

所以在按钮提交时,您将像这样使用它

1
2
3
4
5
[HttpPost]
public ActionResult Add(User user)
 {
   // now user.username has the value that user entered on form
 }