博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net开发细节整理

Posted on 2018-03-03 18:15  火冰·瓶  阅读(137)  评论(0编辑  收藏  举报

1.使用entity framework的DB First时,手动书实体类的表单验证

[MetadataType(typeof(ValidateFund))]
    public partial class Fund
    {
    }

    
    public class ValidateFund
    {
        [Required(ErrorMessage ="必填")]
        public decimal FeeBuy { get; set; }
        public decimal FeeSell { get; set; }
    }

2. MD5加密

        public static string MD5(string str)
        {
            byte[] result = Encoding.Default.GetBytes(str);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
        }

3.绑定asp.net mvc 绑定DropDownList数据

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FundId,MoneyBuy,UnitPriceBuy,Quantity")] MyFund myFund)
        {
            if (ModelState.IsValid)
            {
                myFund.Id = Guid.NewGuid();
                db.MyFunds.Add(myFund);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.FundId = new SelectList(db.FundInfoes, "Id", "Name", myFund.FundId);//赋值
            return View(myFund);
        }

Html页面

@model BCDN.Models.MyFund
@Html.DropDownList("FundId", null, htmlAttributes: new { @class = "form-control" })

4.html页面手动写表单验证

        <div class="form-group">
            <label class="control-label col-md-2">基金名称</label>
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new {
               @class = "form-control",
               @readonly = "readonly",
               data_val = "true",
               @data_val_required = "必填"
           } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>



需要引入的js

<script src="/assets/js/jquery-2.0.3.min.js"></script> 

<script src="/Scripts/jquery.validate.js"></script>

<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

简单的验证规则如下

1.data-val-required 必输字段  
2.data-val-email 必须输入正确格式的电子邮件 
3.data-val-url 必须输入正确格式的网址 
4.data-val-date 必须输入正确格式的日期 
5.data-val-digits: 必须输入正整数
6.data-val-number:必须输入整数

5. ajax从服务端获取数据

<script>
    $(function () {
        $("#buttonJiaoYan").click(function () {
            var code = $("#Code").val();
            var url = "@Url.Action("GetFundNameByCode", "Ajax")?code="+code;
            $.ajax({
                url: url,
                success: function (data) {
                    if (data == "-1") {
                        $("#foundCodeError").css("display", "inline");
                        $("#Name").val('');
                    }
                    else {
                        $("#Name").val(data);
                        $("#foundCodeError").css("display", "none");
                    }
                }
            });
        });
    });
</script>