一、数据访问层(DAL)
public int AddTicketing(Ticketing ticketing) { try { db.Ticketings.Add(ticketing); return db.SaveChanges(); } catch (Exception) { throw; } }
/// <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>
原文:https://www.cnblogs.com/666l/p/15212320.html