从Html.TextBoxFor获得价值 [英] get value from Html.TextBoxFor

查看:149
本文介绍了从Html.TextBoxFor获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本框在我的asp.net MVC的角度值

 <%:的Html .TextBoxFor(M = GT; m.UserName,新{@class =扁平化})%GT; 


解决方案

如果您想从一个表单提交的值,MVC会自动将其绑定到匹配属性名称的任何参数。所以,如果你的形式发布到行动MyPost它是这样的:

 公众的ActionResult MyPost(用户名字符串){//不区分大小写的,你可以做用户名作为以及
VAR一个=用户名;
}

如果你有很多的字段后,您可能需要使用的请求目的。 。MVC将自动绑定使用相同名称的属性。



 公共类MyRequest {
公共字符串用户名{获得;组; }
}

公众的ActionResult MyPost(MyRequest要求)



用户名会在后填充



如果你想用javascript拉出值,你可能会想要把一个ID上,像这样的HTML:

  Html.TextBoxFor(M = GT; m.UserName,新{@class =扁平化,ID =我-文本框 })

然后,你可以使用jQuery或任何你喜欢选择这个名字的元素,假设你希望这一点财产,不会被列举一大堆表单字段。


I want to get the value from text box in my view of asp.net mvc.

 <%: Html.TextBoxFor(m => m.UserName, new { @class = "flat" })%>

解决方案

If you want to get the value from a form post, MVC will automatically bind it to any parameters that match the name of the property. So if you form posted to the action MyPost it would look like this:

public ActionResult MyPost(string UserName) { //Not case sensitive, you can do userName as well
    var a = UserName;
}

If you have lots of fields to post, you might want to use a request object. MVC will automatically bind properties with the same name.

public class MyRequest {
    public string UserName { get; set; }
}

public ActionResult MyPost(MyRequest request)

UserName would be populated on the post.

If you wanted to pull out the values using javascript, you would probably want to put an id on the html like so:

Html.TextBoxFor(m => m.UserName, new { @class = "flat", id = "my-textbox" })

Then you could use jquery or whatever you like to select the element with that name, assuming you wanted just that property and would not be enumerating a bunch of form fields.

这篇关于从Html.TextBoxFor获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆