首页 > 移动平台 > 详细

API+MVC+VUE名称非空非重复、手机正则验证

时间:2021-09-02 06:17:06      阅读:16      评论:0      收藏:0      [点我收藏+]

一、数据访问层(DAL)

  1. 添加
    public int AddTicketing(Ticketing ticketing)
            {
                try
                {
                    db.Ticketings.Add(ticketing);
                    return db.SaveChanges();
                }
                catch (Exception)
                {
    
                    throw;
                }
            }

     

  2. 非重复
    /// <summary>
            /// 查询姓名 0 无重复 1 有重复
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public int GetByName(string name)
            {
                return db.Ticketings.Where(u => u.TicketName == name).FirstOrDefault()==null?0:1;
            }

     

二、控制器

[HttpPost]
        public IHttpActionResult AddTicketing(Ticketing ticketing)
        {
            //判断 是否重复
            if (videoDal.GetByName(ticketing.TicketName) ==0)
            {
                return Json(videoDal.AddTicketing(ticketing));
            }
            else
            {
                return Json("-1");
            }
            
        }

三、MVC

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

四、VUE视图

<div id="app">
    <table class="table table-bordered">
        <tr>
            <td>姓名</td>
            <td>
                <input type="text" v-model="Ticketing.TicketName" name="name" value="" />
            </td>
        </tr>
        <tr>
            <td>手机</td>
            <td>
                <input type="text" v-model="Ticketing.TicketPhone" name="name" value="" />
            </td>
        </tr>
        <tr>
            <td>预约数量</td>
            <td>
                <input type="text" v-model="Ticketing.TicketCount" name="name" value="" />
                
            </td>
        </tr>
        <tr>
            <td>详细地址</td>
            <td><input type="text" v-model="Ticketing.TicketCity" name="name" value="" /></td>

        </tr>
    </table>
    <input type="button" name="name" value="预约" class="btn btn-success" v-on:click="addTicket" />
</div>

<script>
    var MvoieId = location.search.split(=)[1]

    let app = new Vue({
        el: "#app",
        data() {
            return {
                Ticketing: {

                    MovieId: ‘‘,
                    TicketName: ‘‘,
                    TicketPhone: ‘‘,
                    TicketCount: ‘‘,
                    TicketCity: ‘‘
                }
            }
        },
        methods: {
            addTicket() {
                var reg = /^1[3-9]\d{9}$/;
                if (!reg.test(this.Ticketing.TicketPhone)) {
                    alert(手机号错误);
                    return;
                }

                this.Ticketing.MovieId = MvoieId;
                axios.post(http://localhost:55294/api/Video/AddTicketing, this.Ticketing).then(res => {
                    if (res.data === 1) {
                        alert(成功)
                    } else if (res.data === -1) {
                        alert(重名);
                    } else {
                        alert(失败)
                    }
                })
            }
        },
        created: function () {

        }
    })
</script>

API+MVC+VUE名称非空非重复、手机正则验证

原文:https://www.cnblogs.com/666l/p/15212320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!