MVC學習-發送請求

來源:互聯網
上載者:User

標籤:

在HomeControl中添加一個Action,代碼如下:

public ActionResult Add(){     return View();}

當View()中不寫任何參數時,預設會調用同名的視圖,添加一個視圖Add.cshtml,同時添加一個表單,代碼如下:

<body>    <div>        @using (Html.BeginForm())        {            <input type="text" id="id1" name="id1"/><br/>            <input type="text" id="name1" name="name1"/><br/>        <input type="text" id="gender" name="gender"/><br/>        <input type="submit" value="提交"/>          }    </div></body>

通過瀏覽器,可以看到,產生了一個表單,如:

<form action="/Home/Add" method="post">            <input type="text" id="id1" name="id1"/><br/>            <input type="text" id="name1" name="name1"/><br/>        <input type="text" id="gender" name="gender"/><br/>        <input type="submit" value="提交"/>  </form>

可以看到在BeginForm()中未寫參數,預設會提交到當前的後台,post方式請求。跟asp.net很像

既然提交到後台,那麼後台肯定要有個方法接受資料,而且是與請求的視圖同名的,那麼我們接下來就建立一個Action,代碼如下:

[HttpPost]        public ActionResult Add(UserInfo user)        {            int a = 10;//此處設斷點            if (user.Id == 0)            {                int id = int.Parse(Request["Id1"]);                string name = Request["Name1"];                string gender = Request["Gender"];                user = new UserInfo() {Id = id, Name = name, Gender = gender};            }                            return Content("OK");        }

該Action接受post請求,且與原有Add()構成重載,參數為UserInfo 類型的參數,UserInfo類的代碼如下:

namespace _02HtmlHelper.Models{    public class UserInfo    {        public int Id { get; set; }        public string Name { get; set; }        public string Gender { get; set; }    }}

在int a=10,前面設一個斷點,然後選擇【工具】-【添加到進程】-【iisexpress  託管】

然後在重新整理html頁面,再在html中輸入資料,提交表單,這個時候,會自動運行到斷點處,本次測試輸入的資料如:

這個時候,可以看到傳入Add()方法的參數,user中的值如下:

Gender:33
Id:0
Name:null

分析結果,得出知識:

傳入值為:
id1=11;name1=22;gender=33
方法中的UserInfo接收到的參數為
Id=0;Name=null;Gender=33


1.在用戶端中name的大小寫不重要
2.不管有沒有傳入id的值,id總有值,這點很重要
3.當前台發送資料到後台時,mvc內部會自動為把我們把參數拼接成對象,那我們在前台使用對象的話,就可以少寫很多代碼了

接下來修改前台代碼,用對象的方式,因為該視圖在添加的時候,為強型別某種類型,所以要在開頭強型別為UserInfo,代碼如下:

@model _02HtmlHelper.Models.UserInfo@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Add</title></head><body>    <div>        @using (Html.BeginForm())        {            @Html.TextBoxFor(u=>u.Id)<br/>            @Html.TextBoxFor(u=>u.Name)<br/>            @Html.TextBoxFor(u=>u.Gender)<br/>                    <input type="submit" value="提交"/>          }                    </div></body></html>

上面頁面運行後,查看原始碼,如下,對於name=’Id’的部分,會在另外文章中說明:

<form action="/Home/Add" method="post"><input data-val="true" data-val-number="欄位 Id 必須是一個數字。" data-val-required="Id 欄位是必需的。" id="Id" name="Id" type="text" value="" /><br/>         <input id="Name" name="Name" type="text" value="" /><br/>         <input id="Gender" name="Gender" type="text" value="" /><br/>         <input type="submit" value="提交"/>  </form>

按照上面步驟,在int a=10處設立斷點,添加進程,運行後,查看傳入Add(UserInfo user)中user的值,所有的值,後台全部接受到了。

MVC學習-發送請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.