顯示具有 MVC 標籤的文章。 顯示所有文章
顯示具有 MVC 標籤的文章。 顯示所有文章

2016年6月7日 星期二

[C#] [Html AgilityPack] 解析HTML 抓取台灣銀行外匯資料

MVC 架構 當練習 隨便寫寫 有點亂...

功能:
1.抓取外匯資料
2.預設顯示 美金 英鎊 日圓 RMB
3.可供選顯示/隱藏 其他外幣資料


2016年5月11日 星期三

[MVC] 簡易分頁 PagedList

1. 先安裝 NuGet套件 - PagedList.Mvc



























2. Controller

      using PagedList;
      using PageList.Models;

        public ActionResult Index(int page =1)
        {
            //page用來記憶目前是哪一頁
            int currentPage = page < 1 ? 1 : page;

           //資料必須先經過排序
            var product = db.Product.Where(x => x.Status == 1).OrderBy(x => x.ID);

            //分頁資料使用 ToPagedList()
            //參數分別是傳入所要分頁的頁碼以及分頁資料量
            //此範例就是每一頁會有10筆資料
            var PageResult = product.ToPagedList(currentPage, 10);
            

            return View(PageResult);
        }

3. View
















需要using PagedList.Mvc PagedList

取得分頁:

@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page = page}))


































//設定PagedListRenderOptions 可更改樣式
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new  { page = page})
,PagedListRenderOptions.MinimalWithPageCountText)



//也可以自由設定顯示物件
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new  { page = page}),
    new PagedListRenderOptions {
        DisplayLinkToIndividualPages=false,
        DisplayLinkToFirstPage =PagedListDisplayMode.IfNeeded,
        DisplayLinkToLastPage= PagedListDisplayMode.IfNeeded,
        DisplayLinkToNextPage = PagedListDisplayMode.IfNeeded,
        DisplayLinkToPreviousPage = PagedListDisplayMode.IfNeeded,
        DisplayPageCountAndCurrentLocation = true,
        PageCountAndCurrentLocationFormat ="第{0}頁 / 共{1}頁"
    })









https://github.com/TroyGoode/PagedList

2016年2月16日 星期二

在ActionResult中呼叫ActionResult的Function,並接到Json


就是吧! 當我要新增資料 發現 資料庫已經有這筆資料存在時,就改作Update的動作

Update這個Function 已經存在了,所以就想著直接CALL....


喔!!! 這件事就這麼發生了

介紹一下兩個Funtion

新增 --> ActionResult AddNewProduct ,傳回 bool success, String ErroMsg

Update --> ActionResult ProductUpdate 傳回 bool iResult , String Msg

傳回的東西都是 JsonResult , 我想要抓到 ProductUpdate 的Json 回到 AddNewProduct 傳出去


具體怎麼做剛開始還覺得有可能嗎?真的能成嗎?

一方面又覺得若不行 那真的太笨了 =__=

然後我就問了...行呀! 真的能~~


看看片段的實作 CODE

[HttpPost]
public ActionResult AddNewProduct(string ID)
{
   bool iResult = true;
   string msg="";
   bool update = true;
   if (update)
   {
     //把要呼叫的Function ActionResult 轉型成 JsonResult
     JsonResult r = (JsonResult)ProductUpdate(ID,true);

     //因為拿到的r是object 所以再轉一次
     dynamic resultData = r.Data;
     
     //要用的時候,也要給他型別才行
     iResult = (bool)resultData.success;
     if(iResult)
     {
        msg = (String)resultData.FinishMsg;
        msg = msg.Replace("更新", "匯入");   
     }else
     {
       msg = (String)resultData.ErrorMsg;
       msg = msg.Replace("更新", "匯入") + "請重新新增商品";
     }
  }
   else
   {
     //新增資料
    }
 return new JsonResult()
 {
    Data = new { success = iResult , Msg = msg }
 };

}

附上 stackoverflow
還是GOOGLE的高亮程式碼漂亮阿!
其實每次要把CODE放上來我都會腦抽忘了怎麼弄ORZ

2016年1月6日 星期三

[MVC]CheckBox複選、全選、取消

我忘了有沒有寫過類似的東西 剛好今天有空,索性整理一下吧
JQUERY
         // Tab名稱的全選和取消
         $("#AllTab").change(function () {
             if ($(this).is(":checked")) {
                 $("[id*=chkTab] input").prop("checked", true);
             } else {
                 $("[id*=chkTab] input").removeAttr("checked");
             }
         });
         $("[id*=chkTab] input").on("change", function () {

             //全部都選到了,"全部"這個選項打勾,反之,則取消
             if ($("[id*=chkTab] input").length == $("[id*=chkTab] input:checked").length) {
                 $("#AllTab").prop("checked", true);
             }
             else {
                 $("#AllTab").removeAttr("checked");
             }
         });
View 順便講一下好了,checkList 這個是用來記錄CheckBox選取的狀態 MVC 才可以這樣用......
//這段程式碼可以不用理他,是POSTBACK 回來時
//想讓畫面保留勾選的項目
@{ 
    string[] checkList = new string[7];//最後一個存放 全部 的狀態
    if (splitFilter[1] != null)
    {
      string[] splitck = splitFilter[1].TrimEnd(',').Split(',');
      if (splitck[0] != "-1")
      {
         foreach (string i in splitck)
         {
           checkList[int.Parse(i) - 1] = "checked";
          }
      }
                                 
    }
 }

Checkbox是長這樣的

<input type="checkbox" value="1" @checkList[1]><label>AA</label>




 Controller

接到勾選的Tab後,去DB搜尋
之前寫的不太簡潔,順帶記錄一下新寫法

if (Tag != "-1") 
{
  List TagCondition = new List();
  foreach (var t in Tag.Substring(0, Tag.Length - 1).Split(','))
  {
     TagCondition.Add(Convert.ToInt32(t));
   }
   product = product.Where(x =>TagCondition.Contains(x.TabName));
}

2015年10月12日 星期一

[C#][MVC] 資料寫進DB時,檢查是否有Null

create 是要準被塞進DB裡的物件 當create裡面屬性有 null 時 , 就給空值 , 防止DBNull 錯誤發生
IList properties = typeof(ProductSize).GetProperties().ToList();
  foreach (var p in properties)
  {
     if (p.GetValue(create) == null && p.PropertyType == typeof(string))
     {
         p.SetValue(create, "");
      }
  }

2015年10月5日 星期一

[C#]MVC 關於刪除資料RemoveRange Remove

var PTryReport = db.ProductTryReport.Where(x => x.ProductSize_ID == Check.ID);
  if (PTryReport.Any())
  {
    //原本這樣寫 會出錯交易失敗錯誤
    //因為刪除del就會改變PTryReport的值
    foreach (var del in PTryReport)
    {
      db.ProductTryReport.Remove(del);
      db.SaveChanges();
    }
  }
//理想的寫法 用 RemoveRange 一次刪除全部資料
 if (PTryReport.Any())
 {
     db.ProductTryReport.RemoveRange(PTryReport);
     db.SaveChanges();
                                   
  }