For an ASP.NET MVC application, can someone explain to me why calls to Html.BeginForm
begin with the statement @using
?
Example -
@using (Html.BeginForm()) {
//Stuff in the form
}
I thought @using
statements are for including namespaces.
Thanks!
When you use
using
with Html.BeginForm, the helper emits closing tag and opening tag during the call to BeginForm and also the call to return object implementingIDisposable
.When the execution returns to the end of (Closing curly brace) using statement in the view, the helper emits the closing form tag. The
using
code is simpler and elegant.Its not mandatory that you should use
using
in combination withHtml.BeginForm
.You can also use
Using Statement provides a convenient syntax that ensures the correct use of
IDisposable
objects. Since theBeginForm
helper implements theIDisposable
interface you can use theusing
keyword with it. In that case, the method renders the closing</form>
tag at the end of the statement. You can also use theBeginForm
withoutusing
block, but then you need to mark the end of the form:using
as a statements are used to define scope of IDisposable object.Html.BeginForm return object that during dispose render closing tag for a form:
</form>
using
for including namespace is directive.