zoukankan      html  css  js  c++  java
  • MVC 学习日志1(下)

    本篇主要介绍  HtmlHelper,UrlHelper,QueryString传值,Helper表单,UpdateModel,单选·复选(更新)

     注:

    mvc项目新添加的类文件后必须编译后,才能在可视化生成view时候设置基于新添加的类为model

    js文件放在模版页才能生效,在子view里放js不能生效

    上节将MVC 节本类型和MVC 框架结构大致了解下,再结合本章一些方法就可以填充网页内容了,新建Form 然后get ,post数据提交,然后Action中获取并使用。

    1.   HtmlHelper  ,UrlHelper

          MVC 中常用 HtmlHelper 去创建Html元素。

         如;   <%=Html.ActionLink("超级链接到about","about") %>

    2. QueryString 传值 

                   即超链接显示Url?name=mike,点击后在Action中应该能获取 参数id和参数name的值

          生成超链接:<%=Html.ActionLink("链接querystring使用","edit","user",new {name="mike"},new {@class="x"}) %>

          

    image

    userController中的edit Action 获取 参数值有几个方法。

    1) 直接Request.QueryString[“name”]

    2)   public  ActionResult  Edit(string name)      

          只要Action方法中的参数和QueryString传来的参数名字相同就可以直接用。

    3.表单

       QueryString 完成了get方法,那么可以使用post来提交 获取Form了。

       image

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    user/Index页面
    </asp:Content>
    
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    
        <h2>user.Index</h2>
    
    
    
    
    
    
    
    
    
    
    
    
    <%=Html.ActionLink("链接querystring使用","edit","user",new {name="mike"},new {@class="x"}) %>
    
    
    <h2>html.form生成表单</h2>
    <% using (Html.BeginForm("edit","user",FormMethod.Post,new {id="form1"})){ %>
    
    
    Name:<%=Html.TextBox("name") %>
    <br />
    Password:<%=Html.Password("pwd") %>
    
    
    <%-- <%=Html.RadioButton("RadioButton", "d")%>请选择
     <%=Html.CheckBox("CheckBox", "sdfsdf")%>--%>
    <br />
    <input type="submit" id="sub"  value="提交" />
    <% }%>
    
    
    </asp:Content>
    
    

    Html.BeginForm 生成form。

    获取form值也有几个方法:

    1) Request.Form

    2) ACTIONRestult  方法参数和form name 参数相同就行。

       例: Form  中有name,pwd 字段。 那么  ActionResult  edti(string name,string pwd)

    3)用 UpdateModel(new user{xxx},Request.Form.AllKeys);

    4)

            [HttpPost]   //声明当post方法 就用这个action
           public ActionResult Edit( FormCollection collection)

    4.  UpdateModel

    5.单选,多选,并更新

    6.  表单验证

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    user/Index页面
    </asp:Content>
    
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    
        <h2>user.Index</h2>
    
    
        <br />
        <h2>表单提交</h2>
        <fieldset>
    <%=Html.ValidationMessage("u") %>
        <%using (Html.BeginForm())
          { %>
        <label>name</label><%=Html.TextBox("u.name")%>
        <label>pwd</label><%=Html.TextBox("u.pwd")%>
        <br />
    
    
        <input type="submit" value="提交表单" />
    <% } %>
        </fieldset>
    
    
        <br />
    
    
    
    
    
    
    
    
    <%=Html.ActionLink("链接querystring使用","edit","user",new {name="mike"},new {@class="x"}) %>
    
    
    <h2>html.form生成表单</h2>
    <% using (Html.BeginForm("edit","user",FormMethod.Post,new {id="form1"})){ %>
    
    
    Name:<%=Html.TextBox("name") %>
    <br />
    Password:<%=Html.Password("pwd") %>
    
    
    <%-- <%=Html.RadioButton("RadioButton", "d")%>请选择
     <%=Html.CheckBox("CheckBox", "sdfsdf")%>--%>
    <br />
    <input type="submit" id="sub"  value="提交" />
    <% }%>
    
    
    
    
    
    
    
    
    </asp:Content>
    
    

    public ActionResult Index(MvcApplication1.Models.User u)
           {
               //提交表单

               if (string.IsNullOrEmpty(u.name))
               {
                   ViewData.ModelState.AddModelError("u", "姓名不能为空");
               }

               //return RedirectToAction("index", "home");
               //return View("Edit");

               //Response.Redirect("edit");
               return View();
               //return View();
               //return Redirect("home/index");

               //return View("home/index");
           }

    总结:添加1个验证控件

    问题更新和解决方法

    msdn 帮助 VS2008 无 MVC帮助。

    Aspx 无需编译修改就生效

    System.Web.Mvc.HTML

    Prop+tab+tab 自动生成属性

    1.ASP.net MVC 设置默认调试浏览器 http://www.cnblogs.com/hipo/archive/2012/05/09/2491253.html

    2. 

    为什么编译项目 起始  http://localhost:1692/Views/Home/Index.aspx

    属性项目 ---》web-->特定页面--->设置默认 Home/Index

    3自动生成属性

    http://www.360doc.com/content/12/0611/10/9705579_217393104.shtml
    public class User
        {
            public string name { get; set; }
            public string pwd { get; set; };
        }

  • 相关阅读:
    2.Magicodes.NET框架之路——策略管理
    1.Magicodes.NET框架之路——起航
    为什么LINQ to XML的性能要优于XmlDocument?
    SharePoint如何配置Ipad跳转等问题
    写给自己
    ERP,SCM,CRM,BRP,OMS,WMS 企业管理的6大核心系统
    spring 发送邮件问题
    spring各种邮件发送
    css background-position结合disaply:inline-block使用
    .Net 两大利器Newtonsoft.NET和Dapper
  • 原文地址:https://www.cnblogs.com/StudyLife/p/2577194.html
Copyright © 2011-2022 走看看