How do I create a strongly typed BeginForm?

拟墨画扇 提交于 2019-12-02 00:28:01

问题


I've seen a few examples of people using this syntax for HTML.BeginForm:

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

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

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, System.Web.MVC version is v2.0.50727


回答1:


You will find this extension methods in MVCContrib and more specifically in the Microsoft.Web.Mvc.dll assembly in the Microsoft.Web.Mvc.FormExtensions class. So download and include this assembly in your project and add the Microsoft.Web.Mvc namespace in the namespaces section of your 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 will be able to use it in your views.




回答2:


Here is an example, in your .aspx view:

"UserController" being your controller.
"Save()" being your action method in the controller.

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

Hope that helps.



来源:https://stackoverflow.com/questions/2880056/how-do-i-create-a-strongly-typed-beginform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!