Forms in MasterPage and Views

I have a master page which all my views inherit from. The issue I am having is with the form tag which is created in the master page and then the form tag which is created in view.

Because of the form being inside the master page form, all my postbacks are sent to the controllers Index method and its forcing me to create a new method Index which forces an HttpPost.

Further this is causing problems with routes like: /projects/add/ and /projects/delete/1 where everything is router to the Index Method.

WTF? Am i missing something here?

Thanks anyone

Answers


Remove the form from the master page.

Check some sample videos here to better understand the MVC philosophy.

BTW: In ASP.NET MVC, there is no postback.


In my opinion you really ought to think of MasterPages as layouts and not as functional pages. In fact views do not "inherit" from master pages at all. In traditional Webforms, masterpages are in fact user controls. So I would try to not put base-class functionality into a masterpage. So perhaps that is where some of the confusion comes in.


ASP.NET MVC isn't supposed to do "PostBacks"... that's completely missing the point. Remove any "largely-encompasing-form-tags" you have out there, and only put a "form" tag around input fields that are meant (for a specific purpose) to POST to some sort of action.

The fact that you have an all-encompasing form tag (as is true with traditional asp.net) means that you don't have a defined reason for that form, and that is (again) missing the point of MVC.

This is an older article, but it can help with the differences between traditional, and MVC: ASP.NET MVC in the Real World


If you need common form elements on every page, hence the reason you have a form on your master page, then move the form elements to a user control located in the shared folder along with your site.master file. Then include the user control via Html.RenderPartial in each of your view pages. That will only require 1 line of code and maybe a div tag per view which isnt to bad.

BTW is the form located in the master page a login or search control by chance?


Need Your Help

How to setup PHP along Ruby on Rails Apache

php ruby-on-rails ruby apache

Currently I have Apache Webserver running Ruby on Rails. This is config of Rails: