2013-05-22 108 views
2

我正在開發一個應用程序,如博客引擎。在一個主頁的觀點,我有一個鏈接作爲實現博客條目鏈接

<a href="/Blog/BlogEntry/2013/05/22/title for this blog entry" /> 

當鏈接,用戶點擊它會在博客控制器和運行BlogEntry動作有

public class BlogController : Controller { 
    public ActionResult BlogEntry(string title, DateTime createdDate) { 
     // doing something 
     var viewModel = something here for build the view model 

     return View(viewModel); 
    } 
} 

問題是如何我可以這樣做嗎?

回答

2

這樣做,因爲所有參數都將從URL映射。使用動作過濾器以您想要的方式映射數據。我沒有完全測試代碼,但它會給出票價的想法。在您的動作上方添加此屬性。

public ActionResult BlogEntry(int year, int month , int day , string title) 

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 

    namespace OurAttributes 
    { 
    public class PopulateTitleDandDateAttribute : ActionFilterAttribute 
    { 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 

       string[] url = filterContext.HttpContext.Request.Uri.split('/'); 
       DateTime d = new Date(url[2],url[3],url[4]); 
       if (filterContext.ActionParameters.ContainsKey("createdDate")) 
       { 
        filterContext.ActionParameters["createdDate"] = d; 
       } 

       if (filterContext.ActionParameters.ContainsKey("title")) 
       { 
        filterContext.ActionParameters["title"] = url[5] ; 
       } 
       base.OnActionExecuting(filterContext); 


    } 
    } 
} 
+0

感謝您的評論。但我想提交到像BlogEntry(字符串標題,日期時間createdDate)功能。這是我的要求。 – thangchung

+0

@ThangChung我已經更新了我的解決方案,使用actionfilter來填充CreatedDate和標題,只要你想。請看一看。 – Devesh

+0

優秀的人。現在正在工作。謝謝 – thangchung

1

由於Devesh的建議,我在他的代碼修改的東西,它的工作

  • 控制器:

    [HttpGet] 
    [PopulateTitleDandDate] 
    public ActionResult BlogEntry(string title, DateTime createdDate) 
    { 
        var viewModel = new BlogEntryModel 
         { 
          Tittle = title, 
          CreatedDate = createdDate 
         }; 
    
        return View(viewModel); 
    } 
    
  • PopulateTitleDandDateAttribute

    public class PopulateTitleDandDateAttribute : ActionFilterAttribute 
    { 
        public override void OnActionExecuting(ActionExecutingContext filterContext) 
        { 
         var url = filterContext.HttpContext.Request.RawUrl.Split('/'); 
         if (url.Length >= 7) // TODO: it is actually not good here 
         { 
          var d = new DateTime(url[3].ConvertToInteger(), url[4].ConvertToInteger(), url[5].ConvertToInteger()); 
          if (filterContext.ActionParameters.ContainsKey("createdDate")) 
          { 
           filterContext.ActionParameters["createdDate"] = d; 
          } 
    
          if (filterContext.ActionParameters.ContainsKey("title")) 
          { 
           filterContext.ActionParameters["title"] = url[6]; 
          } 
         } 
    
         base.OnActionExecuting(filterContext); 
        } 
    } 
    
  • BlogEntry.cshtml

    @model SampleApplication.Controllers.BlogEntryModel 
    
    <h2>@Html.Raw(Model.Tittle) (@Html.Raw(Model.CreatedDate))</h2> 
    

感謝Devesh一次。