0

Edit: I forgot to mention neither the ActionLink<> or BeginForm<> has worked form me, I have latest MVC version (looked at the available updates for project from NUGET , mvc was not in the recommended list). Is there a sample tutorial/example that uses this feature?

In question "What is an alternative to using strings for action and controller names in asp.net MVC?" I received an answer to use this answer What is the syntax for a strongly-typed ActionLink in MVC 4 with MVC 4 Futures?

I have tried to do what they have done with Action link e.g. :

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

But similar did not even compile :

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

Is there some namespace I need to add or a different syntax I need to try?

Update 2 : This is what I have :

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

UnitTestsController is refereced ok, intellisense wouldn't even allow x => x.Index()) , I had to hammer it in.

Also added System.Linq namespace, but is not being used (grayed out by Reshaper) The error at run time is :

Compiler Error Message: CS0308: The non-generic method 'System.Web.Mvc.Ajax.AjaxExtensions.BeginForm(System.Web.Mvc.AjaxHelper, string, string, System.Web.Routing.RouteValueDictionary, System.Web.Mvc.Ajax.AjaxOptions, System.Collections.Generic.IDictionary)' cannot be used with type arguments

Community
  • 1
  • 1
jimjim
  • 2,414
  • 2
  • 26
  • 46
  • 1
    Which one are using - T4MVC or MVC4 Futures? –  Sep 21 '16 at 03:50
  • @StephenMuecke MVC4 – jimjim Sep 21 '16 at 03:54
  • 1
    Did you include the `Microsoft.Web.Mvc` namespace? And FYI, refer [this answer](http://stackoverflow.com/questions/35329402/system-web-mvc-vs-microsoft-web-mvc/35330351#35330351) for info on the 'Futures' project –  Sep 21 '16 at 03:58
  • 1
    Note you can find the source code for the `BeginForm()` method [here](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/Microsoft.Web.Mvc/FormExtensions.cs) and the `ActionLink()` [here](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/Microsoft.Web.Mvc/LinkExtensions.cs) –  Sep 21 '16 at 04:05
  • 1
    Similar problem here: http://stackoverflow.com/questions/2880056/how-do-i-create-a-strongly-typed-beginform. The standard `BeginForm` provided by `System.Web.Mvc` currently doesn't include type arguments, thus you need to create custom html form helper or use MVC4 Futures. – Tetsuya Yamamoto Sep 21 '16 at 07:57

1 Answers1

1

I don't know if you miss to complete your post but I notice you forgot to "end" the BeginForm? Technically there are two ways to use Html.BeginForm.

Manually end the form :

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

Or for your convenience you can use using() {} to ensure and proper close the form tag:

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

Can you try this approach if this will compile correct for you.

jtabuloc
  • 2,479
  • 2
  • 17
  • 33
  • What namespaces are you using? still no joy – jimjim Sep 21 '16 at 04:45
  • BTW L I was using @using way of BeginForm, did not seem important to include that. – jimjim Sep 21 '16 at 04:58
  • What is the result after your tried this? Have you encounter exception? Have you reference your controller in you view? – jtabuloc Sep 21 '16 at 04:59
  • Added more info at the end of my post, yes controller was refrenced, is there a fully qualified name for generic Html.BeginForm ? I am using VS2013 – jimjim Sep 21 '16 at 05:12