• c#常用代码


    1.超链接:@Html.ActionLink("name","method","controller");   eg:@Html.ActionLink("超链接","Index","Home")  

    2.输出:@{ Response.Write("输出内容") };只能在页面最上面输出

    3.指定位置输出:@Html.Raw("输出内容");

    4.页面横线:</hr>

    5.表单提交:将表单中的数据,以post的方式提交给Home的controller中的index方法中

    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
    @Html.TextBoxFor(p=>p.Id)
    }

    6.异步请求1:

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

    public ActionResult Add(int a,int b)
    {
      int sum = a + b;
      return Content(sum.ToString());
    }

    <script>
      $(function () {
        $('#add').click(function () {
          $.post(
            '@Url.Action("Add","Home")',
            $('#form1').serialize(),
            function (msg) {
              $('#sum').val(msg);
            }
          );
        });
      });

    </script>

    <form id="form1">
    <input type="text" name="a" />+
    <input type="text" name="b" />
    <input type="button" id="add" value="等于" />
    <input type="text" name="sum" id="sum" />
    </form>

    7.异步请求2:

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

    public ActionResult Add1(int a,int b)
    {
      int sum = a + b;
      var temp = new
      {
        Sum = sum
      };
      return Json(temp,JsonRequestBehavior.AllowGet);
    }

    <script>

      function Success(obj) {
        $('#result').val(obj.Sum);
      }
    </script>

    @using (Ajax.BeginForm("Add1", "home", new AjaxOptions()
    {
      OnSuccess = "Success"
    }))
    {
      <input type="text" name="a" />
      <span>+</span>
      <input type="text" name="b" />
      <input type="submit" value="等于" />
      <input type="text" id="result" />
    }

    8.校验

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    @using (Html.BeginForm("Add", "Person", FormMethod.Post))
    {
      @Html.TextBoxFor(p=>p.Id)
      @Html.ValidationMessageFor(p=>p.Id)--校验提示信息
      <br />
      @Html.TextBoxFor(p=>p.Name)
      <br />
      <input type="submit" value="提交" />
    }

    //[Required]//必须的
    //[StringLength:]//字符串长度
    //[Range]//int等范围
    //[RegularExpression]///正则表达式
    //属性ErrorMessage指定错误提示信息

    [Required(ErrorMessage = "编号不能为空")]
    [Range(10,100,ErrorMessage = "编号必须是10到100之间的值")]
    public int Id { get; set; }

  • 相关阅读:
    js 基于函数伪造的方式实现继承
    js 创建List<Map> 这种格式的集合
    微信get post请求到微信服务器 模版 素材操作
    微信开发学习 问题1: 网页授权问题 “该连接无法访问” 解决方法
    Jackson 高性能的JSON处理 ObjectMapper
    baseDao 使用spring3+hibernate4方式
    PropertiesUtil 读取配置文件工具类
    C语言(函数)学习之strstr strcasestr
    命令行选项解析函数(C语言):getopt()和getopt_long()
    AE插件开发的一些总结
  • 原文地址:https://www.cnblogs.com/kcwang/p/15236651.html
Copyright © 2020-2023  润新知