MVC Html.BeginForm using Areas

asp.net MvcHtml Helper

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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" }))

Solution 2 - asp.net Mvc

Try this:

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

Solution 3 - asp.net Mvc

Try specifying the area, controller, action as RouteValues

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

Solution 4 - asp.net Mvc

Use this for area with HTML Attributes

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

}

Solution 5 - asp.net Mvc

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

Solution 6 - asp.net Mvc

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" })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarikoView Question on Stackoverflow
Solution 1 - asp.net MvcmosesfettersView Answer on Stackoverflow
Solution 2 - asp.net MvcNam LeView Answer on Stackoverflow
Solution 3 - asp.net MvctvanfossonView Answer on Stackoverflow
Solution 4 - asp.net MvcSamJackSonView Answer on Stackoverflow
Solution 5 - asp.net MvcMohammad KarimiView Answer on Stackoverflow
Solution 6 - asp.net MvcSumesh EsView Answer on Stackoverflow