How to create a strongly typed BeginForm?

I saw several examples of using this syntax for HTML.BeginForm:

(Html.BeginForm<Type>(action => action.ActionName(id)))

But when I try this syntax, all I get is:

The non-generic method System.Web.Mvc.Html.FormExtensions.BeginForm(System.Web.Mvc.HtmlHelper)'
cannot be used with type arguments

What am I missing? Visual Studio 2010 version, System.Web.MVC v2.0.50727

+3
source share
2 answers

You will find these extension methods in MVCContrib and more specifically in the assembly Microsoft.Web.Mvc.dllin the class Microsoft.Web.Mvc.FormExtensions. Therefore, download and include this assembly in your project and add a namespace Microsoft.Web.Mvcin the section of namespacesyour web.config file:

<namespaces>
    <add namespace="System.Web.Mvc"/>
    <add namespace="System.Web.Mvc.Ajax"/>
    <add namespace="System.Web.Mvc.Html"/>
    <add namespace="System.Web.Routing"/>
    <add namespace="System.Linq"/>
    <add namespace="System.Collections.Generic"/>
    <add namespace="Microsoft.Web.Mvc"/>
</namespaces>

and you can use it in your ideas.

+11
source

Here is an example in your .aspx view:

"UserController", .
" Save()" - .

<%
using (Html.BeginForm<UserController>(x => x.Save(null, null, Model.User.ID, null, null), FormMethod.Post, new { id = "formUser" })) {
%>
   <%= Html.AntiForgeryToken() %>
   <%: Html.ValidationSummary(true) %>
   ...
<% } %>

, .

-1

Source: https://habr.com/ru/post/1746518/


All Articles