2013-05-22 113 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一次。