2012-01-20 76 views
1

我使用MVC中 - 剃鬚刀 - 我是很新,它和我有一點麻煩產生一個網格內的ActionLink的...MVC剃刀 - 用ActionLink的錯誤網格

@grid.GetHtml(
      //Some grid setting stuff here 
      columns: grid.Columns(
       grid.Column(null, null, 
        @<div class="vehicleResult"> 

          @{ 
           var text = string.Format("{0} {1} {2}", item.Value.GetPropertyValue("Manufacturer"), item.Value.GetPropertyValue("Shell Shape"), item.Value.GetPropertyValue("Model")); 
           @Html.ActionLink(text, MVC.Search.ActionNames.VehicleView, MVC.Search.Name, new { Id = item.Id }, new { }); 
          } 

         <a>@string.Format("{0} {1} {2}", item.Value.GetPropertyValue("Manufacturer"), item.Value.GetPropertyValue("Shell Shape"), item.Value.GetPropertyValue("Model"))</a> 
      ))) 

的<一>末標籤工作正常,但我想從一個<>標籤的文字即

@string.Format("{0} {1} {2}", item.Value.GetPropertyValue("Manufacturer"), item.Value.GetPropertyValue("Shell Shape"), item.Value.GetPropertyValue("Model")) 
通過發送到ActionLink的

(以上位)。

另外,如果我做...

var text = "blah"; 
@Html.ActionLink(text, MVC.Search.ActionNames.VehicleView, MVC.Search.Name, new { Id = item.Id }, new { }); 

那麼這很好 - 它只是當他們在一起這是行不通的。我得到的錯誤是...

System.Web.Mvc.HtmlHelper<GIT.RetailWebsite.App.ViewModels.VehicleSearchModel> 
has no applicable method named 'ActionLink' but appears to have an extension 
method by that name. Extension methods cannot be dynamically dispatched. 
Consider casting the dynamic arguments or calling the extension method 
without the extension method syntax.  
d:\TFSProjects\Retail Website\Main\src\Retail Website\GIT.RetailWebsite.App\Views\Search\_grsResultsGrid.cshtml 31 GIT.RetailWebsite.App 

任何人都可以協助嗎?

回答

3

由於您嘗試在擴展方法中使用dynamictext變量(ActionLink),並且在調用擴展方法時不支持動態參數,因此會引發異常。
text變量是dynamic,因爲它是由dynamicitem參數構造而成的。

雖然異常消息一點點神祕它描述了你可以做什麼:

鑄dymanic說法:

@Html.ActionLink((string)text, 
    MVC.Search.ActionNames.VehicleView, 
    MVC.Search.Name, new { Id = item.Id }, new { }); 

或撥打擴展方法爲靜態方法:

@LinkExtensions.ActionLink(Html, text, 
    MVC.Search.ActionNames.VehicleView, 
    MVC.Search.Name, new { Id = item.Id }, new { });