.NET MVC入门(routine配置,取前台参数,跳转,校验,页面缓存)

 



1. Global.asax,配置路由

            routes.MapRoute("Flow","Flow/{action}/{uId}/{flowId}",new { Controller="Flow",action="FlowIndex",uId="-1",flowId="-1"});


设置默认的启动项  eg: 默认的启动项为 /Movie/Index/1
  routes.MapRoute(
           "Default", // 路由名称
           "{controller}/{action}/{currPage}/{movieId}", // 带有参数的 URL
           new { controller = "Movie", action = "Index", currPage = 1, movieId = UrlParameter.Optional } // 参数默认值
       );

 

2. Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPro.BLL;
using MvcPro.Models;

namespace MvcPro.Controllers
{
    public class FlowController : Controller
    {
        //
        // GET: /Flow/

        public ActionResult FlowIndex()
        {
            return View();     //将返回与方法同名的aspx页面上  如本方法将跳转到 FlowIndex.aspx
        }

        public ActionResult ViewFlow(Int32 uId)
        {
            //取出流程
            FlowService flowService = new FlowService();
            List<FlowHeaderVo> flowList = flowService.getCurrFlow(uId);
            ViewData["flowList"] = flowList;
            return View();
        }

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

        public ActionResult NewFlowOP(FlowHeaderVo flowVo) {
            ViewData["flowVo"] = flowVo;
            return View("NewFlowSuccess");
          
        }  

        //返回json
        public ActionResult getFlowJosn(Int32 uId)      //返回json
        {
            FlowService flowService = new FlowService();
            List<FlowHeaderVo> flowList = flowService.getCurrFlow(uId);
            return Json(flowList,JsonRequestBehavior.AllowGet);
        }

    }
}


3.  对于有数据提交的页面 Inherits 指向Model,将直接将表单封装为一个Model对象


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcPro.Models.FlowHeaderVo>" %>
<%@ Import Namespace="System.Linq" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 NewFlow
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>NewFlow</h2>
   <form  action="/Flow/NewFlowOP">
     <fieldset>
    <table>
    <tr>
    <td>流程编号</td>
      <td><%: Html.TextBoxFor(m=>m.floeCode) %>
      </td>
    </tr>
      <tr>
    <td>申请人</td>
      <td><%: Html.TextBoxFor(m=>m.appUserName) %>
      </td>
    </tr>
      <tr>
    <td>申请时间</td>
      <td><%: Html.TextBoxFor(m=>m.appDate) %>
      </td>
    </tr>
      <tr>
    <td>当前状态</td>
      <td><%: Html.TextBoxFor(m=>m.currStatus) %>
      </td>
    </tr>
    </table>
    </fieldset>
    <input type="submit" />
</form>
</asp:Content>


4.  后台Controller,将对象以参数传入即可


     public ActionResult NewFlowOP(FlowHeaderVo flowVo) {
            ViewData["flowVo"] = flowVo;
            return View("NewFlowSuccess");
          
        }  

 

 


两种取数方式
1. 直接取字段
2. 封装对象


1./File/FieldTest

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>FieldTest</h2>
    <form action="/File/FieldSubmit">
    <p>fileName:<input  type="text" name="fileName"/></p>
     <p>fileType:<input  type="text" name="fileType"/></p>
     <p>fileSize:<input  type="text" name="fileSize"/></p>
      <p><input  type="submit" name="fileSize" value="submit"/></p>
    </form>

    <br />
    <%=ViewData["fileName"]  %><br />
      <%=ViewData["fileType"]%><br />
        <%=ViewData["fileSize"]%><br />
</asp:Content>


 public ActionResult FieldTest() {
            return View("FieldTest");
        }

        public ActionResult FieldSubmit(string fileName, string fileType, int fileSize)
        {
            ViewData["fileName"] = fileName;
            ViewData["fileType"] = fileType;
            ViewData["fileSize"] = fileSize;
            return View("FieldTest");
        }


2.  /File/FieldTest2


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApp.Models.FieldModel>" %>
<%@ Import Namespace="MvcApp.Models" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <form action="/File/FieldSubmit2">
    <p>fileName:<input  type="text" name="fileName"/></p>
     <p>fileType:<input  type="text" name="fileType"/></p>
     <p>fileSize:<input  type="text" name="fileSize"/></p>
      <p><input  type="submit" name="fileSize" value="submit"/></p>
    </form>

    <br />
    <%
        FieldModel fieldModel = (FieldModel)ViewData["fieldModel"] ;
        if(null != fieldModel){
        %>
        <%=fieldModel.fileName %><br>
       <%=fieldModel.fileType %><br>
          <%=fieldModel.fileSize %><br>
        <%
}
        %>
</asp:Content>

 

public ActionResult FieldTest2()
        {
            return View("FieldTest2");
        }


        //直接封装一个对象出来,对象的field和页面field一致
        public ActionResult FieldSubmit2(FieldModel fieldModel)
        {
            ViewData["fieldModel"] = fieldModel;
            return View("FieldTest2");
        }

 

  public class FieldModel
    {
        public string fileName { get; set; }
        public string fileType { get; set; }
        public int fileSize { get; set; }
    }

 


  3.  使用mvc对表单域进行校验

前台页面中加


   <%using(Html.BeginForm()){
        %>

            <p>fileName:<input  type="text" name="fileName"/><%=Html.ValidationMessage("fileName") %></p>
            <p>fileType:<input  type="text" name="fileType"/><%=Html.ValidationMessage("fileType")%></p>
            <p>fileSize:<input  type="text" name="fileSize"/><%=Html.ValidationMessage("fileSize")%></p>
            <input type="submit" value="submit"/>
        <%
            Html.EndForm();
     } %>

 

 

   在Model上加如下的校验信息

    public class FieldModel
    {
        [Required(ErrorMessage="请输入文件名称.")]
        public string fileName { get; set; }
        [RegularExpression("[^(.exe||.bat)]$",ErrorMessage="不能上传exe,bat文件")]
        public string fileType { get; set; }
        [RegularExpression("\\d+",ErrorMessage="必须输入数字.")]
        public int fileSize { get; set; }
    }

  

设置页面缓存

[OutputCache(Duration=60,VaryByParam="")]
        public ActionResult FieldTest3()
        {
          return View();
        }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sust2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值