您的位置:首页 > 编程语言 > ASP

AspNet MVC4 教学-5:AspNet MVC4 页面动态生成演示

2015-03-24 11:46 204 查看
HomeControllers.cs文件内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcDynamicPage.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

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

        [HttpPost]     
        public ActionResult DynamicOutput(string RowNum)
        {
            int iRowNum;
            if (int.TryParse(RowNum, out iRowNum) == false)
            {
                ViewBag.Err = "IntTryErr";
                return View("Err");
            }
            if (iRowNum < 0)
            {
                ViewBag.Err = "RowNum < 0";
                return View("Err");
            }
            ViewBag.RowNum = iRowNum;
            return View();
        }
    }
}


Index.cshtml内容:

@{
    ViewBag.Title = "Index";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
</head>
<body>
<h2>Index</h2>
@using (Html.BeginForm("DynamicOutput", "Home"))
{
    @Html.TextBox("RowNum");
     <input type="submit" value="提交" />
}
</body>
</html>


DynamicOutput.cshtml内容:

@{ 
    int iRowNum = (int)ViewBag.RowNum;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生花名册-共@(iRowNum)个</title>
</head>

<body>
<table width="400" border="1" align="center">
  <caption>
 学生花名册</caption>
 <thead style="color:#000080;background-color:#BFBFFF"> 
 <tr>
    <th>编  号</th>
    <th>姓  名</th>
    <th>年  龄</th>
 </tr>
  </thead>
  <tbody>
  @{
  for(int i=0;i<iRowNum;i++)
  {
  <tr>
    <td>@(i+1)</td>
    <td> </td>
    <td>  </td>
  </tr>
  }
  }
  </tbody>
</table>
</body>
</html>


Err.cshtml内容:

@{
    ViewBag.Title = "Err";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
</head>
<body>
<h2>Err</h2>
@ViewBag.Err;
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: