asp.net mvc 3 - Rewriting Html.BeginForm() in MVC 3.0 and keeping unobtrusive javascript -


this going seem bit of silly endeavor, it's want learn nonetheless.

right now, in asp.net mvc 3.0, need use syntax @using (html.beginform()) { , later, } close form block fancy new 'unobtrusive javascript', lest want write of hand (which fine).

for reason (read: *ocd*) don't that. i'd rather this..

@html.beginform() <div class="happy-css"> </div> @html.endform() 

seem stupid yet? yeah, each own. want understand why working how , mold liking. thought first place start digging mvc 3.0 source itself. jumped codeplex find beginform extension method.

( http://aspnet.codeplex.com/sourcecontrol/changeset/view/63452#288009 )

so little confused how begin achieving goal. reading through code, discovered go down root method (not surprising, extension methods seem hierarchical methods reaching down single 1 avoid redundancy).

private static mvcform formhelper(this htmlhelper htmlhelper, string formaction, formmethod method, idictionary<string, object> htmlattributes) {             tagbuilder tagbuilder = new tagbuilder("form");             tagbuilder.mergeattributes(htmlattributes);             // action implicitly generated, htmlattributes take precedence.             tagbuilder.mergeattribute("action", formaction);             // method explicit parameter, takes precedence on htmlattributes.             tagbuilder.mergeattribute("method", htmlhelper.getformmethodstring(method), true);              httpresponsebase httpresponse = htmlhelper.viewcontext.httpcontext.response;             httpresponse.write(tagbuilder.tostring(tagrendermode.starttag));             return new mvcform(htmlhelper.viewcontext.httpcontext.response);         } 

what not seeing here how method relates unobtrusive javascript. if type out ..

<form action="/controller/action" method="post">

and put in validation so...

@html.validationsummary(false)

i not unobtrusive javascript. if use

@using (html.beginform()) { do. i've examined generated markup , can't find difference.

now gets strange. if type in ...

@html.beginform() , put of form code, form works , unobtrusive javascript, have manually type in </form> @ end. @html.endform() doesn't work. ontop of that, text system.web.mvc.html.mvcform written output stream right beneath <form action="/controller/action" method="post"> html.

can enlighten and/or me?

the answer underlying question (i.e. how use beginform/endform syntax) in following manner:

@{ html.beginform(...); } <div> content</div> @{ html.endform(); } 

unfortunately razor syntax right bit more verbose when invoking helpers write output (as opposed majority of helpers return html snippet). make easier writing own extension methods so:

public static ihtmlstring formbegin(this htmlhelper helper, ...) {     helper.beginform(...);     return new htmlstring(""); }  public static ihtmlstring formend(this htmlhelper helper) {     helper.endform();     return new htmlstring(""); } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -