53

I'm using MVC areas and on a view that's in an area called "Test" I would like to have a form that posts to the following method:

area: Security
controller: AccountController
method: logon

How can I make this happen with Html.BeginForm? Can it be done?

0

6 Answers 6

95

For those of you that want to know how to get it to work with the default mvc4 template

@using (Html.BeginForm("LogOff", "Account", new { area = ""}, FormMethod.Post, new { id = "logoutForm" }))
1
  • 6
    +1 for the additional anonymous type with 'area', rather than adding 'area' to the anonymous type with 'id', as I had tried.
    – Jeff
    Dec 31, 2013 at 0:05
85

Try this:

Html.BeginForm("logon", "Account", new {area="Security"})
0
7

Try specifying the area, controller, action as RouteValues

@using (Html.BeginForm( new { area = "security", controller = "account", action = "logon" } ))
{
   ...
}
5

Use this for area with HTML Attributes

@using (Html.BeginForm(
      "Course", 
      "Assign", 
      new { area = "School" }, 
      FormMethod.Get, 
      new { @class = "form_section", id = "form_course" })) 
{

   ...

}
4
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "logoutForm", action = "/Account/LogOff" }))
                {@Html.AntiForgeryToken()
                    <a class="signout" href="javascript:document.getElementById('logoutForm').submit()">logout</a>
                }
2

For Ajax BeginForm we can use this

Ajax.BeginForm("IndexSearch", "Upload", new { area = "CapacityPlan" }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = updateTarget }, new { id = "search-form", role = "search" })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.