ASP.NET MVC 4 Controller + View ( Razor ) + Cross Action Form Post + Form Collection 使用方法

 
ASP.NET MVC 4 Controller + View ( Razor ) + Cross Action Form Post + Form Collection 使用方法
 

   /Home/Form ( /Views/Home/Form.cshtml )
 
   <table>
 
      @using (@Html.BeginForm("FormResult", "Home"))
      {
 
         <tr>
            <td>Value 1 :</td>
            <td><input type="text" name="val1" value="" /></td>
         </tr>
 
         <tr>
            <td>Value 2 :</td>
            <td><input type="text" name="val2" value="" /></td>
         </tr>
 
         <tr>
            <td>Value 3 :</td>
            <td><input type="text" name="val3" value="" /></td>
         </tr>
 
         <tr>
            <td colspan="2">
               <input type="submit" name="Submit" value="Submit" /> 
            </td>
         </tr>
 
      }
 
   </table>
 
   /Home/FormResult ( /Views/Home/FormResult.cshtml )
 
   <table border="0" cellpadding="3" cellspacing="0" width="100%">
 
      <tr>
         <td>Value 1 :</td>
         <td>@Html.Raw(ViewBag.Val1)</td>
      </tr>
 
      <tr>
         <td>Value 2 :</td>
         <td>@Html.Raw(ViewBag.Val2)</td>
      </tr>
 
      <tr>
         <td>Value 3 :</td>
         <td>@Html.Raw(ViewBag.Val3)</td>
      </tr>
 
   </table>
 
   /Controller/HomeController.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
 
   namespace MvcApplication.Controllers
   {
      public class HomeController : Controller
      {
         … …
 
         public ActionResult Form()
         {
            … …
 
            return View();
         }
 
         public ActionResult FormResult(FormCollection collection)
         {
            ViewBag.Val1 = collection["val1"];
            ViewBag.Val2 = collection["val2"];
            ViewBag.Val3 = collection["val3"];
 
            return View();
         }
      }
   }