简体   繁体   English

如何从Html.TextBox MVC 2 asp.net C#检索值?

[英]How do I retrieve values from Html.TextBox MVC 2 asp.net C#?

I'm working on a school project and I need some help. 我正在做一个学校项目,需要一些帮助。 I've created a form and I want to get the submitted values from it. 我已经创建了一个表单,我想从中获取提交的值。 Is it possible to do this without using JavaScript? 是否可以在不使用JavaScript的情况下做到这一点? And in that case, how do I do it? 在这种情况下,我该怎么做?

Form: 形成:

<div id="secondRowInputBox">
        <% using (Html.BeginForm("Index","Home",FormMethod.Post))
        {%>
            <%= Html.TextBox("Id")%> <br />
            <%= Html.TextBox("CustomerCode") %><br />
            <%= Html.TextBox("Amount") %><br />
            <input type="submit" value="Submit customer data" />
        <%} %>
    </div>

Just create an HttpPost action in your controller accepting the form values as parameters: 只需在控制器中创建一个HttpPost动作,将表单值作为参数即可:

[HttpPost]
public ActionResult Index(int id, string customerCode, int amount)
{
    // You can change the type of the parameters according to the input in the form.
    // Process data.    
}

You might want to look into model binding . 您可能需要研究模型绑定 This allows you to create strongly-typed views and saves you the trouble of creating actions with dozens of parameters. 这使您可以创建强类型的视图,并省去了创建带有多个参数的操作的麻烦。

You have already done half the work, now in home controller make an actionresult 您已经完成了一半的工作,现在在家用控制器中进行操作

[HttpPost]
public ActionResult Index(int id, string customerCode, int amount)
{
// work here.
}

form post method will call this method, as you have specified it in the begin form parameters. 正如您在begin form参数中指定的那样,form post方法将调用此方法。

It will be better if you use a model for passing values and use it in view for form elements 如果您使用模型传递值并在表单元素的视图中使用它会更好

[HttpPost]
public ActionResult Index(ModelName modelinstance)
{
// work here.
}

Sample loginModel 样本loginModel

public class LoginModel
{
    [Required]
    [Display(Name = "Username:")]
    public String UserName { get; set; }

    [Required]
    [Display(Name = "Password:")]
    [DataType(DataType.Password)]
    public String Password { get; set; }
}

now if was using this login model in the form 现在,如果在表单中使用此登录模型

then for the controller action, modelinstance is simply the object of model class 然后对于控制器操作,modelinstance只是模型类的对象

[HttpPost]
public ActionResult Index(LoginModel loginDetails)
{
// work here.
}

if you have a lot of variables in the form then having a model helps as you don't need to write for all the properties. 如果表单中有很多变量,则无需编写所有属性,因此拥有模型会有所帮助。

Henk Mollema's answer is good. Henk Mollema的答案很好。 Here to say something more on it. 这里要说更多。

Html.TextBox will generate html like below one, there's a name attribute. Html.TextBox将生成类似于以下内容的html,其中有一个name属性。

<input id="CustomerCode" name="CustomerCode" type="text">

When you submit the form, all the values of input fields can be get from Request.Form by name attribute as key Request.Form["CustomerCode"] , and ASP.NET MVC has done some magic for us, so it can simply go into the param of the action method. 提交表单时,所有输入字段的值都可以从Request.Form通过name属性作为键Request.Form["CustomerCode"] ,并且ASP.NET MVC为我们做了一些魔术,因此可以轻松地进行操作进入动作方法的范式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM