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

时间:2016-06-27 05:47:09

标签: c# asp.net-mvc asp.net-mvc-routing

我正在尝试在Web表单中添加一个视图,并且可以很好地显示数据。但我做了一个发布它的帖子。我使用了LINK样本,我在沙发上发现但它不适用于POST请求。总是抛出MAC失败错误。

1 个答案:

答案 0 :(得分:0)

Webforms通常使用单个表单作为整个页面。但是HTML does not support nested forms,所以如果您通过MVC局部视图将另一个表单放到页面上,则需要确保部分视图不会在<form runat="server">标记内呈现。

Here is an example在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>
  

注意:如果您的主要网页表单是在母版页中声明的,则需要在单独的ContentPlaceHolder控件内呈现MVC表单,以便表单不会嵌套。< / p>

<!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>