2016-09-21 45 views
0

編輯:我忘了提及既沒有ActionLink <>或BeginForm <>已經工作過我,我有最新的MVC版本(查看可用的更新來自NUGET的項目,mvc不在推薦列表中)。有沒有使用此功能的示例教程/示例?Html.BeginForm(「Index」,「CustomersController」)不使用字符串

在問題「What is an alternative to using strings for action and controller names in asp.net MVC?」我收到的答案,使用此答案What is the syntax for a strongly-typed ActionLink in MVC 4 with MVC 4 Futures?

我試圖做什麼,他們有例如動作鏈接完成:

@(Html.ActionLink<CustomersController>(x => x.Index(), "Customers")) 

但類似的甚至沒有編譯:

Html.BeginForm<CustomersController>(x => x.Index()) 

有一些命名空間,我需要添加或不同的語法我需要嘗試?

更新2:這是我有:

@using (Html.BeginForm<UnitTestsController>(x => x.Index())) 

UnitTestsController是refereced OK,智能感知甚至不會讓X => x.Index()),我不停地拍它

還增加System.Linq命名空間,但是沒有被使用(通過整形器變灰) 在運行時的錯誤是:

編譯器錯誤消息:CS0308:非通用方法「System.Web.Mvc.Ajax。 AjaxExtensions.BeginForm(System.Web.Mvc.Aj axHelper,字符串,字符串,System.Web.Routing.RouteValueDictionary,System.Web.Mvc.Ajax.AjaxOptions,System.Collections.Generic.IDictionary)'不能與類型參數一起使用

+1

哪一個正在使用 - T4MVC或MVC4期貨? –

+0

@StephenMuecke MVC4 – Arjang

+1

您是否包含'Microsoft.Web.Mvc'命名空間?關於「期貨」項目的信息,請參閱[本答案](http://stackoverflow.com/questions/35329402/system-web-mvc-vs-microsoft-web-mvc/35330351#35330351) –

回答

1

我不知道你是否想念完成你的文章,但我注意到你忘了「結束」BeginForm?技術上有兩種使用方法Html.BeginForm

手動結束方式:

@{ Html.BeginForm<CustomersController>(x => x.Index()); } 
    <input type="text" id="txtQuery"/> 
    <input type="submit" value="submit"/> 
@{ Html.EndForm(); } 

或者爲了您的方便,你可以使用using() {}保證和適當緊密的表單標籤:

@using (Html.BeginForm<CustomersController>(x => x.Index())) 
{ 
    <input type="text" id="txtQuery"/> 
    <input type="submit" value="submit"/>  
} 

你可以試試這個方法,如果這將彙編正確爲你。

+0

你在使用什麼命名空間?仍然沒有快樂 – Arjang

+0

順便說一句我正在使用@使用BeginForm的方式,似乎並不重要。 – Arjang

+0

你試過這個後的結果是什麼?你有沒有遇到異常?你有沒有參考你的控制器? – jtabuloc