1. 程式人生 > 實用技巧 >君仙的收貨地址

君仙的收貨地址

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using BLL;
 7 using MODEL;
 8 
 9 namespace WT01.Controllers
10 {
11     public class AddressController : Controller
12     {
13         // GET: Address
14         public ActionResult Index()
15 { 16 return View(); 17 } 18 19 public ActionResult Add() 20 { 21 return View(); 22 } 23 24 //修改 25 public ActionResult Edit(int id) 26 { 27 ViewBag.id = id; 28 return View(); 29 }
30 31 //獲取收貨地址List 32 [HttpGet] 33 public JsonResult GetList() 34 { 35 ShAddressBLL bll = new ShAddressBLL(); 36 return Json(bll.GetList(), JsonRequestBehavior.AllowGet); 37 } 38 39 40 //刪除 41 [HttpPost] 42 public JsonResult Del(int
id) 43 { 44 ShAddressBLL bll = new ShAddressBLL(); 45 return Json(bll.Del(id)); 46 } 47 48 //新增 49 [HttpPost] 50 public JsonResult ShAddressAdd(ShAddress obj) 51 { 52 ShAddressBLL bll = new ShAddressBLL(); 53 return Json(bll.Add(obj)); 54 } 55 56 57 //獲取區域List 58 [HttpGet] 59 public JsonResult GetAreaList(int pid) 60 { 61 AreaBLL bll = new AreaBLL(); 62 return Json(bll.GetList(pid), JsonRequestBehavior.AllowGet); 63 } 64 65 //修改 66 [HttpPost] 67 public JsonResult ShAddressEdit(ShAddress obj) 68 { 69 ShAddressBLL bll = new ShAddressBLL(); 70 return Json(bll.Edit(obj)); 71 } 72 73 //根據ID獲取地址資訊--->反填 74 [HttpGet] 75 public JsonResult GetShAddressByID(int id) 76 { 77 ShAddressBLL bll = new ShAddressBLL(); 78 return Json(bll.GetShAddressByID(id), JsonRequestBehavior.AllowGet); 79 } 80 81 82 } 83 }
Controller
 1 <script>
 2 
 3     //文件就續函式
 4     $(function () {
 5         InitLoad();
 6     })
 7 
 8     //刪除
 9     function del(id) {
10         $.ajax({
11             url: '/Address/Del',
12             type: 'post',
13             dataType: 'json',
14             data: {id:id},
15             success: function (data) {
16                 InitLoad();
17             }
18         })
19     }
20 
21     //導航新增頁面
22     function AddLocation() {
23         location.href = '/Address/Add';
24     }
25 
26     //導航修改頁面
27     function editLocation(id) {
28         location.href = '/Address/Edit?id='+id;
29     }
30 
31     //初始化載入資料
32     function InitLoad() {
33         $.ajax({
34             url: '/Address/GetList',
35             type: 'get',
36             dataType: 'json',
37             success: function (data) {
38                 $("#divMain").empty();
39                 $(data).each(function () {
40                     var div = '<div style="border:solid;color:blue;margin-top:6px">';
41                     div += '收貨人:' + this.sName + '<img style="float:right;cursor:pointer;" onclick="del(' + this.Id+')" title="刪除" src="../Images/del.jpg" /><br />';
42                     div += '所在地區:' + this.Province + this.City+'<br />';
43                     div += '地址:' + this.dzhi+'<br />';
44                     div += '手機:' + this.handset + '<a style="float:right;" href="javascript:editLocation(' + this.Id+')">編輯</a>';
45                     div += '</div>';
46                     $("#divMain").append(div);
47                 })
48             }
49         })
50     }
51 
52 </script>
Show
 1 @{
 2     ViewBag.Title = "Add";
 3     Layout = null;
 4 }
 5 <script src="~/Scripts/jquery-3.3.1.min.js"></script>
 6 <script>
 7 
 8     //文件就續函式
 9     $(function () {
10         InitLoadData();    
11     })
12 
13     //載入省或市
14     function InitLoadData() {
15         $.ajax({
16             url: '/Address/GetAreaList',
17             type: 'get',
18             dataType:'json',
19             data: { pid: 0 },
20             success: function (data) {
21                 $("#selProvince").empty();
22                 $(data).each(function (index) {
23                     $("#selProvince").append('<option value = "' + this.Id + '" > ' + this.Name + '</option >');
24                     if (index == 0)
25                         ChangeLoadData(this.Id);
26                 })
27             }
28         })
29     }
30 
31     //載入市或區
32     function ChangeLoadData(pid) {
33         $.ajax({
34             url: '/Address/GetAreaList',
35             type: 'get',
36             dataType: 'json',
37             data: { pid: pid },
38             success: function (data) {
39                 $("#selCity").empty();
40                 $(data).each(function () {
41                     $("#selCity").append('<option value ="' + this.Id + '" > ' + this.Name + '</option>');
42                 })
43             }
44         })
45     }
46 
47 
48     //新增
49     function Add() {
50         var obj = {
51             sName:$("#txtName").val(),         
52             Province: $("#selProvince option:selected").text(),
53             City: $("#selCity option:selected").text(),
54             dzhi:$("#txtAddress").val(),
55             handset:$("#txtHandset").val()
56         };
57         $.ajax({
58             url: '/Address/ShAddressAdd',
59             type: 'post',
60             dataType: 'json',
61             data: obj,
62             success: function (data) {
63                 location.href = '/Address/Index';
64             }
65         })
66     }
67 
68 
69     //返回
70     function Prev() {
71         location.href = '/Address/Index';
72     }
73 </script>
74 
75 <h2>收貨地址新增</h2>
76 <table>
77     <tr>
78         <td align="right">收貨人:</td>
79         <td><input type="text" id="txtName" /></td>
80     </tr>
81     <tr>
82         <td  align="right">所在地區:</td>
83         <td>省<select onchange="ChangeLoadData(this.value)" id="selProvince">
84             </select>市區<select id="selCity"></select></td>
85     </tr>
86     <tr>
87         <td  align="right">地址:</td>
88         <td><input type="text" id="txtAddress" /></td>
89     </tr>
90     <tr>
91         <td  align="right">手機號:</td>
92         <td><input type="text" id="txtHandset" /></td>
93     </tr>
94     <tr>
95         <td colspan="2"  align="center"><input id="btnSave" value="儲存" onclick="Add()" type="button" />&nbsp;<input id="btnReturn" value="返回" onclick="Prev()" type="button" /></td>       
96     </tr>
97 </table>
Add
  1 @{
  2     ViewBag.Title = "Add";
  3     Layout = null;
  4 }
  5 <script src="~/Scripts/jquery-3.3.1.min.js"></script>
  6 <script>
  7 
  8     //文件就續函式
  9     $(function () {
 10         var id = "@ViewBag.id";
 11         $("#hdID").val(id);
 12         GetAddressInfoByID(id);//調反填過程
 13         InitLoadData();
 14     })
 15 
 16     //載入省或市
 17     function InitLoadData() {
 18         $.ajax({
 19             url: '/Address/GetAreaList',
 20             type: 'get',
 21             dataType:'json',
 22             data: { pid: 0 },
 23             success: function (data) {
 24                 $("#selProvince").empty();
 25                 $(data).each(function (index) {
 26                     $("#selProvince").append('<option value = "' + this.Id + '" > ' + this.Name + '</option >');
 27                     if (index == 0)
 28                         ChangeLoadData(this.Id);
 29                 })
 30             }
 31         })
 32     }
 33 
 34     //載入市或區
 35     function ChangeLoadData(pid) {
 36         $.ajax({
 37             url: '/Address/GetAreaList',
 38             type: 'get',
 39             dataType: 'json',
 40             data: { pid: pid },
 41             success: function (data) {
 42                 $("#selCity").empty();
 43                 $(data).each(function () {
 44                     $("#selCity").append('<option value ="' + this.Id + '" > ' + this.Name + '</option>');
 45                 })
 46             }
 47         })
 48     }
 49 
 50 
 51     //修改
 52     function Edit() {
 53         var obj = {
 54             Id: $("#hdID").val(),//這裡非常關健
 55             sName:$("#txtName").val(),
 56             Province: $("#selProvince option:selected").text(),
 57             City: $("#selCity option:selected").text(),
 58             dzhi:$("#txtAddress").val(),
 59             handset:$("#txtHandset").val()
 60         };
 61         $.ajax({
 62             url: '/Address/ShAddressEdit',
 63             type: 'post',
 64             dataType: 'json',
 65             data: obj,
 66             success: function (data) {
 67                 location.href = '/Address/Index';
 68             }
 69         })
 70     }
 71 
 72 
 73     //反填
 74     function GetAddressInfoByID(id) {
 75         $.ajax({
 76             url: '/Address/GetShAddressByID',
 77             type: 'get',
 78             dataType: 'json',
 79             data: { id: id },
 80             success: function (data) {               
 81                 $("#hdID").val(data.Id);
 82                 $("#txtName").val(data.sName);
 83                 //$("#selProvince option[value='" + '5' + "']").attr("selected", true); 
 84                 //$("#selProvince").find("option[text='安徽']").attr("selected", true);
 85                 $(".aa option[text='" + '安徽' + "']").attr("selected", true); 
 86                 $("#selProvince").attr("value", '5'); 
 87                 $("#selCity option[value='" + '7' + "']").attr("selected", true);              
 88                 $("#txtAddress").val(data.dzhi);
 89                 $("#txtHandset").val(data.handset);               
 90             }
 91         })
 92     }
 93 
 94 
 95     //返回
 96     function Prev() {
 97         location.href = '/Address/Index';
 98     }
 99 
100 </script>
101 
102 <h2>收貨地址修改</h2>
103 <table>
104     <tr>
105         <td align="right">收貨人:</td>
106         <td><input type="text" id="txtName" /><input id="hdID" type="hidden" /></td>
107     </tr>
108     <tr>
109         <td align="right">所在地區:</td>
110         <td>
111             省<select class="aa" onchange="ChangeLoadData(this.value)" id="selProvince"></select>市區<select id="selCity"></select>
112         </td>
113     </tr>
114     <tr>
115         <td align="right">地址:</td>
116         <td><input type="text" id="txtAddress" /></td>
117     </tr>
118     <tr>
119         <td align="right">手機號:</td>
120         <td><input type="text" id="txtHandset" /></td>
121     </tr>
122     <tr>
123         <td colspan="2" align="center"><input id="btnSave" value="儲存" onclick="Edit()" type="button" />&nbsp;<input id="btnReturn" value="返回" onclick="Prev()" type="button" /></td>
124     </tr>
125 </table>
Update

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using MODEL;
 7 using System.Data.Entity;
 8 
 9 namespace DAL
10 {
11    public  class ShAddressDAL
12     {
13         public List<ShAddress> GetList()
14         {
15             using (Model1 mc = new Model1())
16             {
17                 return mc.ShAddresss.ToList();
18             }
19         }
20 
21         //刪除
22         public int Del(int id)
23         {
24             using (Model1 mc = new Model1())
25             {
26                 ShAddress ss = mc.ShAddresss.Find(id);
27                 mc.ShAddresss.Remove(ss);
28                 return mc.SaveChanges();
29             }
30         }
31 
32         //新增
33         public int Add(ShAddress obj)
34         {
35             using (Model1 mc = new Model1())
36             {
37                 mc.ShAddresss.Add(obj);
38                 return mc.SaveChanges();
39             }
40         }
41 
42         //修改
43         public int Edit(ShAddress obj)
44         {
45             using (Model1 mc = new Model1())
46             {
47                 mc.Entry<ShAddress>(obj).State = EntityState.Modified;
48                 return mc.SaveChanges();
49             }
50         }
51 
52         //根據ID獲取地址資訊
53         public ShAddress GetShAddressByID(int id)
54         {
55             using (Model1 mc = new Model1())
56             {
57                 return mc.ShAddresss.Find(id);
58             }
59         }
60 
61 
62     }
63 }
ShAddressDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;

namespace DAL
{
    public class AreaDAL
    {
        public List<Area> GetList(int pid)
        {
            using (Model1 mc = new Model1())
            {
                return mc.Areas.Where(x => x.Pid.Equals(pid)).ToList();
            }
        }
    }
}
AreaDAL