如何在Web表单中调用视图并在该视图中执行Post

本文关键字:视图 执行 Post 表单 调用 Web | 更新日期: 2023-09-27 17:49:26

我试图添加一个视图内的Web表单和它的fine显示数据。但我做了一个post - it break。我已经使用了我在软件上发现的链接样本,但它不会为POST请求工作。总是抛出MAC失败错误

如何在Web表单中调用视图并在该视图中执行Post

Webforms通常为整个页面使用单个表单。但是HTML不支持嵌套表单,所以如果你通过MVC局部视图将另一个表单放到页面上,你需要确保局部视图不会在你的<form runat="server">标签内呈现。

这是一个在Webforms中使用多个表单的例子。

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <fieldset>
    <legend>ASP.NET web form (POST)</legend>
        <asp:Label runat="server" AssociatedControlID="txtSearch">Name: </asp:Label><asp:TextBox runat="server" ID="txtSearch" /><asp:Button runat="server" ID="btnSend" Text="Search" OnClick="btnSend_Click" />
    </fieldset>
    </form>
    <form method="get" action="Search.aspx">
        <fieldset>
        <legend>Regular HTML form using GET</legend>
            <label for="name-text">Name: </label><input type="text" id="name-text" name="q" /><input type="submit" value="Search" />
        </fieldset>
    </form>
    <form method="post" action="Search.aspx">
        <fieldset>
        <legend>Regular HTML form using POST</legend>
            <label for="name-text2">Name: </label><input type="text" id="name-text2" name="q" /><input type="submit" value="Search" />
        </fieldset>
    </form>
</body>
</html>

注意:如果你的主webforms表单是在一个母版页面中声明的,你需要在一个单独的ContentPlaceHolder控件中呈现你的MVC表单,这样表单就不会嵌套。

<!DOCTYPE HTML>
<html id="Html1" runat="server" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml-rdfa-1.xsd" xmlns:og="http://opengraphprotocol.org/schema/" >
<head id="Head1" runat="server">
    <title>My Site</title>
</head>
<body id="Body1" runat="server">
    <form id="frmMain" runat="server">
        <!-- form for Webforms -->
        <asp:ContentPlaceHolder id="MainContent" runat="server">
        </asp:ContentPlaceHolder>
    </form>
    <!-- placeholder for external forms -->
    <asp:ContentPlaceHolder ID="OutsideOfForm" runat="server">
    </asp:ContentPlaceHolder>
</body>
</html>