MVC Controller 與 View 傳值

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   使用   ar   for   

Controller 到 View1 強型別

控制器

        // GET: /Test/        public ActionResult Index()        {            DateTime date = DateTime.Now;  //大部分時候使用自訂類型            return View(date);        }

視圖

@model DateTime@{ViewBag.Title = "Index";}<h2>Index</h2><h2>The time is: @Model.ToString("yyyy年MM月dd日 HH:mm:ss");</h2><h2>The day is: @(((DateTime)Model).DayOfWeek)  </h2><h2>The day is: @Html.TextBoxFor(m=>m.DayOfYear)</h2>
2  ViewBag,ViewData,TempData

控制器(注意第二項,第三項輸出結果)

public ActionResult TempDataTest()        {            TempData["Message"] = "TempData Message";            ViewData["Message"] = "ViewData Message";            ViewBag.Mesaage = "ViewBag Message";            ViewBag.Info = "ViewBag Message";            return View();        }

視圖

@{    ViewBag.Title = "TempDataTest";}<h2>TempDataTest</h2><h2>@TempData["Message"]</h2><h2>@ViewData["Message"]</h2><h2>@ViewBag.Message</h2><h2>@ViewBag.Info</h2>
View 到 Controller

1 QueryString

視圖

@{    ViewBag.Title = "QueryStringTest";}<h2>@ViewData["Message"]</h2><h2>QueryStringTest</h2><h2> @Html.ActionLink("TestController", "QueryStringTest", new { word = "word" }) </h2>

控制器

public ActionResult QueryStringTest(string Word)        {            ViewData["Message"] = Request.QueryString["Word"];            ViewData["Message1"] = Word;            return View();        }

2 Post

視圖

@{    ViewBag.Title = "PostTest";}<h2>PostTest</h2><h2>@TempData["Message"]</h2><div>    //第一種
    <form action="/Test/PostTest" method="post">        <ul>            <li>使用者名稱:<input type="text" value="" name="UserName" /></li>            <li>密  碼:<input type="text" value="" name="Password" /></li>        </ul>        <input type="submit" value="提交" />    </form>
    //第二種    @using (Html.BeginForm("PostTest", "Test", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))    {        @Html.AntiForgeryToken()        <p>            <div class="form-group">                @Html.Label("使用者名稱", new { @class = "col-md-2 control-label" })                <div class="col-md-10">                    @Html.TextBox("username", "", new { @class = "form-control" })                </div>            </div>            <div class="form-group">                @Html.Label("密碼", new { @class = "col-md-2 control-label" })                <div class="col-md-10">                    @Html.TextBox("Password", "", new { @class = "form-control" })                </div>            </div>            <div class="form-group">                <div class="col-md-offset-2 col-md-10">                    <input type="submit" value="提交" class="btn btn-default" />                </div>            </div>        </p>    }</div>

控制器

public ActionResult PostTest()        {            return View();        }        [HttpPost]        public ActionResult PostTest(string UserName, string Password)        {            TempData["Message"]= "username ="+UserName +"  Password="+Password;            return View();        }

MVC Controller 與 View 傳值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.