2

在 MVC/Orchard 应用程序中使用 Razor cshtml 文件。

表单区域读取为:

@using (Html.BeginForm("Action", "Controller", FormMethod.Get, new { area = "Area.area"})) {
    Html.AntiForgeryToken();

    @Html.LabelFor(model => model.Value)
    @Html.EditorFor(model => model.Value)

    <div class="actions">
    @Html.ActionLink("Do Something", "OtherAction", "Controller", new { area = "Area.area"}, new { @class = "button"})
    <button type="submit">Submit</button>
}

这会产生以下 HTML:

<form action="/Location/URL" method="post">
    <!---Editor Fields etc-->
    <div class="actions">
        <a class="button" href="/Location/OtherURL">OtherAction</a>
        <button type="submit">Submit</button>
    </div>
</form>

正如你所看到的,这个表单被分配了POST方法,据我所知,这与在Html.BeginForm的FormMethod参数中使用FormMethod.Get相反。

我在测试另一种形式时注意到的一件事(真实的形式要长得多,所以我插入了一个非常小的形式来查看语法/行为是否已检出)如下:

@using (Html.BeginForm("Action", "Controller", FormMethod.Get, new {area = "Area.area"})) {
    <button value="submit">Test</button>
}

@using (Html.BeginForm("Action", "Controller", FormMethod.Get, new { area = "Area.area"})) {
    Html.AntiForgeryToken();

    @Html.LabelFor(model => model.Value)
    @Html.EditorFor(model => model.Value)

    <div class="actions">
    @Html.ActionLink("Do Something", "OtherAction", "Controller", new { area = "Area.area"}, new { @class = "button"})
    <button type="submit">Submit</button>
}

这导致了以下结果:

<form action="/Location/URL" method="post">
    <button value="submit">Test</button>
    <form action="/Location/URL" area="Area.area" method="get">
        <!---Editor Fields etc-->
        <div class="actions">
            <a class="button" href="/Location/OtherURL">OtherAction</a>
            <button type="submit">Submit</button>
        </div>
    </form>
</form>

所以这似乎使第一个表单成为 POST,第二个表单成为 GET,但将第二个表单放在第一个表单内。

任何人都可以解释一下吗?

4

0 回答 0