MVC4商城項目三:分部視圖在導航條上的應用

來源:互聯網
上載者:User

標籤:style   blog   http   os   io   資料   for   2014   

寫了幾天發覺大部分時間用在JS上了,本來想寫個musicstore,卻加了架構,然後又想用後台,然後又想用上bootstrapt,然後又想弄許可權設計,然後又想…………

看來是想多了~

好吧,最近把後台搭起來了,用了metronic.bootstrap, 真心很強大。功能很多,為了節約時間成本就在它上面改吧。先看看

 

架構示範地址:http://dreamsky.github.io/main/blog/metronic-bootstrap/index.html

:http://dreamsky.github.io/main/blog/metronic-bootstrap/metronic.bootstrap.rar

來自分享:夢想天空

 

要實現導航條和功能表列的功能,我用到了分部視圖,就當是WEBFORM的使用者控制項來理解吧。

 先看一下實體模型:

 public class UrlConfigDto    {        public int Id { get; set; }        public string UrlName { get; set; }        public string Url { get; set; }        public int PageLev { get; set; }        public int LevOrder { get; set; }        public int UrlRole { get; set; }        public string Icon { get; set; }        public int POrder { get; set; }     }

  

我想把<URL,UNAME>綁定在視圖上,所以我的Service層返回的是List<string>

 public List<string> GetMeunTool(int orderId)        {            List<string> dic = new List<string>();          UrlConfig urlConfig=  _urlConfigRepository.GetFiltered(a => a.LevOrder == orderId).FirstOrDefault();            if (urlConfig==null)            {                return null;            }            dic.Add(urlConfig.UrlName+"^"+urlConfig.Url+"/"+urlConfig.LevOrder);            for (int i = 0; i < urlConfig.PageLev-1; i++)            {                UrlConfig tem = _urlConfigRepository.GetFiltered(a => a.LevOrder == urlConfig.POrder).FirstOrDefault();                dic.Add(tem.UrlName + "^" + tem.Url+ "/" + tem.LevOrder );                urlConfig.POrder = tem.LevOrder;            }            return dic;        }

建立分部視圖,傳遞資料模型:

  public PartialViewResult MeunTool(int id)        {            return PartialView(_urlService.GetMeunTool(id));        }

來看看視圖頁面

@model List<string>@if (Model == null){    <h3 class="page-title">首頁</h3>    <ul class="breadcrumb">        <li>            <i class="icon-home"></i>            <a href="/Admin/Admin">首頁</a>        </li>    </ul>}else if (Model.Count == 1){    <h3 class="page-title">@Model[0].Split(‘^‘)[0] </h3>    <ul class="breadcrumb">        <li>            <i class="icon-home"></i>            <a href="/Admin/Admin">首頁</a>            <i class="icon-angle-right"></i>        </li>        <li><a href="@Model[0].Split(‘^‘)[1]">@Model[0].Split(‘^‘)[0]</a></li>    </ul>}

其實我這裡想用Dictionary<string,string> 的,不過PartialView()裡不允許。

在模板頁面我們就可以這樣調用分部視圖了:

  @{         int id = Convert.ToInt32(Html.ViewContext.RouteData.Values["id"]);         Html.RenderAction("MeunTool", "Admin", new { id = id });   }

 

說明一下這裡的id的設計作用 ,這個ID是urlconfig表的惟一鍵,我用它來識別當前頁的位置,如URL:http://localhost:11903/Admin/Admin/UserInfo/301  

 @Html.ViewContext.RouteData.Values["id"]  在視圖頁面可以直接接受到id傳參

 

PS:想要當做項目做而不一個DEMO,在使用者體驗得下大功夫,JS是最耗時間的,有點拖垮人的節奏……

具體代碼的實現,請參見 https://code.csdn.net/denghao156/ktnmb_mvc4

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.