• 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; }

  • 相关阅读:
    西门子1200/1500 PLC FC/FB块的区别
    Monaco Editor --Web编辑器 自定义主题、代码提示等
    C# 强制GC垃圾回收
    C# 注册表操作类(查询、修改、删除)
    WinForm重绘Combobox控件无边框样式
    Http-server搭建本地服务
    C# 压缩解压文件夹
    递归获取当前节点和所有父节点
    递归获取当前父节点下的所有子集
    轻量级的通信引擎 StriveEngine
  • 原文地址:https://www.cnblogs.com/kcwang/p/15236651.html
Copyright © 2020-2023  润新知