ASP.NET mvc5-@ {Html.BeginForm();} vs @using(Html.BeginForm())

时间:2018-07-12 03:16:16

标签: razor asp.net-mvc-5

对于MVC专家来说,这可能是一个简单的问题,但是在谷歌搜索时我找不到答案。

我正在读一本有关ASP.NET mvc的书,作者说,建议在创建表单时使用@using,因为它将在末尾添加</form>。但是,当我仅使用@{ Html.BeginForm();}测试没有代码的代码时,它添加了</form>。当与实现IDisposable接口的数据库或对象一起使用时,我了解@using,但是在这种情况下,如果它的用法与根本不使用时相同,为什么甚至将其用于表单?

使用@using的代码:

 @using (Html.BeginForm())
    {
        @Html.ValidationSummary()
        <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
        <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
        <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
        <p>
            Will you attend?
            @Html.DropDownListFor(x => x.WillAttend, new[] {
            new SelectListItem() {Text = "Yes, I'll be there",
            Value = bool.TrueString},

            new SelectListItem() {Text = "No, I can't come",
            Value = bool.FalseString}
            }, "Choose an option")
        </p>
        <input type="submit" value="Submit RSVP" />
    }

不带@using的代码:

 @{ Html.BeginForm(); }
        @Html.ValidationSummary()
        <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
        <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
        <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
        <p>
            Will you attend?
            @Html.DropDownListFor(x => x.WillAttend, new[] {
            new SelectListItem() {Text = "Yes, I'll be there",
            Value = bool.TrueString},

            new SelectListItem() {Text = "No, I can't come",
            Value = bool.FalseString}
            }, "Choose an option")
        </p>
        <input type="submit" value="Submit RSVP" />

两者都将添加结尾</form> 感谢您的任何见解。

0 个答案:

没有答案
相关问题