加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – html表单发布到mvc控制器

发布时间:2020-12-16 01:58:02 所属栏目:百科 来源:网络整理
导读:我正在尝试设置一个简单的登录html页面,其操作被发送到我的另一个网站上的mvc控制器.我没有问题设置页面来做帖子,在mvc控制器中我有我的方法来读取表单帖子.问题是我没有在表单集合中看到html表单中的字段. 在mvc控制器方法中读取表单帖需要做些什么特别的事
我正在尝试设置一个简单的登录html页面,其操作被发送到我的另一个网站上的mvc控制器.我没有问题设置页面来做帖子,在mvc控制器中我有我的方法来读取表单帖子.问题是我没有在表单集合中看到html表单中的字段.

在mvc控制器方法中读取表单帖需要做些什么特别的事情,如果是这样的话是什么?

这是我页面中的表单操作标记

<form action="http://reconciliation-local.sidw.com/login/launch" method="post">
    User Name <input type="text" id="username"/><br/>
    Password <input type="text" id="password"/>
    <input type="submit" value="launch"/>


</form>

控制器方法

[HttpPost]
    public ActionResult launch(FormCollection fc)
    {
        foreach (string fd in fc)
        {
            ViewData[fd] = fc[fd];
        }
        return View();
    }

当我单步执行控制器方法代码时,我在formcollection参数中没有看到任何内容.

解决方法

将Html发布到MVC控制器

>使用表单创建HTML页面(不要忘记引用Jquery.js)

????
????
????

????

<form id="myform" action="rec/recieveData" method="post">

    User Name <input type="text" id="username" name="UserName" /><br />
    Password  <input type="text" id="password" name="Password"/>
              <input type="submit" id="btn1" value="send" />
</form>

<script>
    $(document).ready(function () {

         //get button by ID
        $('#btn1').submit(function () {

            //call a function with parameters
            $.ajax({
                url: 'rec/recieveData',//(rec)= Controller's-name 
                                         //(recieveData) = Action's method name
                type: 'POST',timeout: '12000',(optional 12 seconds)
                datatype: 'text',data: {
                    //Get the input from Document Object Model
                    //by their ID
                    username: myform.username.value,password: myform.password.value,}

            });
        });
    });

</script>

然后在MVC控制器中

controller/action
                             |        |

1.创建名为rec的rec(rec / recieveData)

>创建名为rec.cshtml的视图

这是控制器:

public class recController : Controller
    {
        // GET: rec
        string firstname = "";
        string lastname = "";

        List<string> myList = new List<string>();

        public ActionResult recieveData(FormCollection fc)
        {
          //Recieve a posted form's values from  parameter fc
            firstname = fc[0].ToString(); //user
            lastname = fc[1].ToString();  //pass

           //optional: add these values to List
            myList.Add(firstname);
            myList.Add(lastname);

            //Importan:
            //These 2 values will be return with the below view
            //using ViewData[""]object...
            ViewData["Username"] = myList[0];
            ViewData["Password"] = myList[1];

            //let's Invoke view named rec.cshtml
            // Optionaly we will pass myList to the view
            // as object-model parameter,it will still work without it thought
            return View("rec",myList);
        }
    }

这是视图:

@{
    ViewBag.Title = "rec";
}

<h2>Hello from server</h2>

<div>
    @ViewData["Username"]<br /> <!--will display a username-->
    @ViewData["Password"] <!-- will display a password-->

</div>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读