jdavis
jdavis

Reputation: 8655

ASP.NET MVC3 Create a Form Element using HTML.BeginForm with specific attributes

I am trying to create a form in my view that will route to a specific controller and action, that will do so as a Get request, and that will have a class attribute "pull-right"

This is what I've tried so far...

 using (Html.BeginForm("LogOff", "Account", "Get", null, new {@class = "pull-right"}))
 {
     <div class="clearfix">
         <label> In as: <strong>@User.Identity.Name</strong></label>
     </div>
     <button class="btn" type="submit">Log Out</button>
 }

But it it is throwing an error and I can't figure out how to correctly create this method. Any help on this would be appreciated.

Upvotes: 1

Views: 2197

Answers (2)

dknaack
dknaack

Reputation: 60468

Description

The right overload of the BeginForm method should, in your case, this

  BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, 
            FormMethod method, object htmlAttributes);

Pass in the Get using the enum FormMethod.Get

Sample

 @using (Html.BeginForm("LogOff", "Account", FormMethod.Get, new {@class = "pull-right"}))
 {
     <div class="clearfix">
         <label> In as: <strong>@User.Identity.Name</strong></label>
     </div>
     <button class="btn" type="submit">Log Out</button>
 }

More Information

Upvotes: 5

Steve Mallory
Steve Mallory

Reputation: 4283

The third argument, FormMethod, does not take a string, but an enum. Try FormMethod.Get.

Upvotes: 1

Related Questions