将值从文本框传递到控制器 [英] passing value from textbox to controller

查看:113
本文介绍了将值从文本框传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从文本框"EmailList"中获取价值并将其发送给控制器?我一直在使用webforms,这是我第一次接触mvc.

How can I get value from textbox "EmailList" and send it to controler? I'm always using webforms, and this is my first contact with mvc.

查看:

    @Html.TextBox("EmailList")
    @Html.Action("SendEmails")

控制器:

    public ActionResult SendEmails()
    {

        // some operations on EmailList
    }

编辑


如果我只需要打开简单的方法"onclick",该怎么办?没有actionresult.例如-

EDIT


And what if I need just to open simple method 'onclick'? Not actionresult. for example -

       public void SendEmails()
        {
            // some operations on EmailList
        }

推荐答案

因此,要将值返回到控制器中,您首先需要发出 POST ,这样您将想要为 POST 设置控制器操作:

So to get the value back into the controller you're going to need to issue a POST first of all so you'll want to setup your controller action for a POST:

[HttpPost]
public ActionResult SendEmails(string EmailList)
{
}

还请注意,我添加了一个参数 EmailList ,其名称与表单上的控件完全相同.接下来,我们需要确保您的HTML设置正确,因此在构建 form 控件时,应按以下步骤进行构建:

also notice I added a parameter EmailList that's named exactly the same as the control on the form. Next we need to make sure your HTML is setup right, so when you build the form control build it like this:

@Html.BeginForm("SendEmails", "{ControllerNameHere}", FormMethod.Post)

然后是您的文本框,请放开它,它应该可以正常工作.

and then your text box, well leave it alone, it should work just fine.

这篇关于将值从文本框传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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