Is it mandatory to name view and action mehod same in mvc?

izengod

I have a view name EmployeeForm.cshtml and inside EmployeeController I have two action method MyView() and RandomView(). MyView() returns object of Employee Model while RandomView() method returns the object of EmployeeViewModel.

But the URl request goes as like: http://localhost:port/Employee/EmployeeForm i.e. MVC expects an EmployeeForm() method inside EmployeeController.

How can i correct this error ?

Orel Eraki

No it's not.

You view form should point to the name desired, whatever it's the current MyView() or another custom name by decorating the controller action with attribute.

No matter what you choose, the view form must match the action name(or attribute name).

[ActionName("EmployeeForm")]
public ActionResult MyView()
{
    // You code here
    return View(); // Will use MyView page
    return View("SomeViewName"); // Will use SomeViewName page
}

Concerning View page, this is almost not related. You can set the controller action to return whatever View page you want using View("SomeViewName", myModel).

Note: When using BeginForm without passing the desired action, it will use the current View file page as the target form Url action name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In MVC is there any Mandatory for Declaring View Name and Action Method are Same?

From Dev

Is it mandatory to return the view name from controller in spring mvc?

From Dev

How to specify the view name in an MVC action?

From Dev

Handle Multiple action with same name in MVC

From Dev

Action name in MVC same as corresponding http verb

From Dev

Name a view/action 'View' in ASP.NET MVC without modifing routes

From Dev

Name a view/action 'View' in ASP.NET MVC without modifing routes

From Dev

MVC Action Link in partial view

From Dev

MVC Action Link in partial view

From Dev

How to modify ASP MVC default view name associated to a controller action method?

From Dev

How to modify ASP MVC default view name associated to a controller action method?

From Dev

How to display Get and Post action result in same view in mvc5?

From Dev

Multiple actions with same action name

From Dev

ASP.NET MVC Routing - Redirect to Route when Action Name and Url is same

From Dev

ASP.NET MVC two controllers with same name, how to specify view location

From Dev

Materialized view and table with the same name

From Dev

Pass a list of view models to mvc controller action

From Dev

Pass Query string to mvc view action

From Dev

Passing Parameters from View to Action in MVC

From Dev

Pass Query string to mvc view action

From Dev

Get attribute of current controller action in MVC View

From Dev

MVC action name exposed when javascript disabled

From Dev

Action name is null in spring 4 mvc

From Dev

Invoking Same Name Controller Action for Mobile Devices

From Dev

Invoking Same Name Controller Action for Mobile Devices

From Dev

Redirect_to a different view using the same action as another view

From Dev

How to use same RESTFUL name/route with same action?

From Dev

Is it mandatory to name groups and folders in English?

From Dev

Is it mandatory to name groups and folders in English?

Related Related

  1. 1

    In MVC is there any Mandatory for Declaring View Name and Action Method are Same?

  2. 2

    Is it mandatory to return the view name from controller in spring mvc?

  3. 3

    How to specify the view name in an MVC action?

  4. 4

    Handle Multiple action with same name in MVC

  5. 5

    Action name in MVC same as corresponding http verb

  6. 6

    Name a view/action 'View' in ASP.NET MVC without modifing routes

  7. 7

    Name a view/action 'View' in ASP.NET MVC without modifing routes

  8. 8

    MVC Action Link in partial view

  9. 9

    MVC Action Link in partial view

  10. 10

    How to modify ASP MVC default view name associated to a controller action method?

  11. 11

    How to modify ASP MVC default view name associated to a controller action method?

  12. 12

    How to display Get and Post action result in same view in mvc5?

  13. 13

    Multiple actions with same action name

  14. 14

    ASP.NET MVC Routing - Redirect to Route when Action Name and Url is same

  15. 15

    ASP.NET MVC two controllers with same name, how to specify view location

  16. 16

    Materialized view and table with the same name

  17. 17

    Pass a list of view models to mvc controller action

  18. 18

    Pass Query string to mvc view action

  19. 19

    Passing Parameters from View to Action in MVC

  20. 20

    Pass Query string to mvc view action

  21. 21

    Get attribute of current controller action in MVC View

  22. 22

    MVC action name exposed when javascript disabled

  23. 23

    Action name is null in spring 4 mvc

  24. 24

    Invoking Same Name Controller Action for Mobile Devices

  25. 25

    Invoking Same Name Controller Action for Mobile Devices

  26. 26

    Redirect_to a different view using the same action as another view

  27. 27

    How to use same RESTFUL name/route with same action?

  28. 28

    Is it mandatory to name groups and folders in English?

  29. 29

    Is it mandatory to name groups and folders in English?

HotTag

Archive