如何在控制器中访问文本框值. [英] how to access textbox value in controller.

查看:89
本文介绍了如何在控制器中访问文本框值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试开发一个asp.net mvc网站.我想在表格内的同一页面上检索文本框值.
如何访问该值.

Hi,
I am trying to develop one asp.net mvc website. where i want to retrieve the textbox value on same page which is inside the table.
How to access that value.

推荐答案

您无法从Controller访问TextBox,MVC则无法正常工作.如果需要该值,则必须将包含TextBox的表单提交给Controller.以下链接显示了一种实现该目标的方法:

http://www.asp.net/mvc/tutorials/mvc-music-store-第5部分 [^ ]
You can''t access the TextBox from the Controller, MVC doesn''t work that way. you''ll have to submit the form containing the TextBox to the Controller instead if you want the value. The link below shows a way of achieving that:

http://www.asp.net/mvc/tutorials/mvc-music-store-part-5[^]


访问控制器中的文本框值取决于控制器方法将采用的参数.

假设您的控制器仅接受一个参数,然后只需将其与参数一起重定向到控制器方法名称的Url即可调用它.

Accessing textbox value in controller depends on the parameters your controller method will take.

Suppose your controller will take only one argument then you simply call it by redirecting to the Url of your controller''s method name along with argument.

{Controller}/{Method Name}/{Argument}
Employee/Details/EmpID
Employee/Details/12

//controller method looks like
Public ActionResult Details(int EmployeeId)
{ ..... }



或者,如果您的控制器将模式参数作为参数,则准备json对象并将其传递给控制器​​.使用它,您还可以将多个值传递给控制器​​.



Or if your controller will take modal as parameter then prepare json object and pass it to the controller. Using it you can also pass multiple values to controller.

//Javascript code
var employee = { EmployeeId: 12,
                 EmployeeName: StartShining }
  
//Your modal class looks like
Public Class Employee 
{
 Public int EmployeeId { get; set; }
 Public string EmployeeName { get; set; }
}

//Your controller method looks like
Public ActionResult Details(Employee emp)
{ ..... }



HTH



HTH


在Aspx页面中添加此代码
--------------------------------
Add this code in Aspx page
--------------------------------
First Name
<%: Html.TextBoxFor(m=>m.FirstName)%>
Last Name
<%: Html.TextBoxFor(m=>m.LastName)%>
<input type="Submit" value="Submit" />



将此添加到模型



Add this to Model

Public string FirstName{get; Set;};
Public string LastName{get; Set;};



在Controller中,您可以访问



In Controller u can access

.FirstName


这篇关于如何在控制器中访问文本框值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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