如何在ASP.Net MVC中表单提交并显示不同的页面?

时间:2010-04-08 16:09:19

标签: jquery asp.net-mvc forms

我是asp.net mvc的新手..

所以基本上我只是建立了一个两页应用程序,它接收用户的注册信息并将其发布到数据库。

我使用大量的jquery和ajax调用来使用linq to sql stored proc对象从数据库中检索数据。

目前我被困在一个页面上,在用户提交表单后,它应该将其重定向到/ Home / ProductAdded。

我发现的是错误:

 Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

我的表单上使用的基本上是html控件,asp.net控件和一些asp.net mvc类型控件的组合。

我使用action =“/ Home / ProductAdded”提交表单 在做了一些谷歌搜索后,我发现我应该添加机器密钥,但这样做后,索引页面变得不可见。因为它现在无法找到索引文件。

删除动作有帮助,但现在它不会去任何地方。 我在这里失踪了什么?

我觉得我错过了很多关于asp.net mvc的基础知识,我甚至不知道如何提交表格并转到其他页面!!

编辑:包括我的index.aspx

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

    <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.jsonSuggest.js" type="text/javascript"></script>
    <script src="Scripts/ProductRegistration.js" type="text/javascript"></script>
    <script src="Scripts/jquery.Guid.js" type="text/javascript"></script>

    <script src="Scripts/jquery.scrollTo-min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.ui.core.min.js" type="text/javascript"></script>

    <script src="Scripts/jquery.ui.datepicker.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.colorbox-min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="Scripts/date.js" type="text/javascript"></script>

    <form id="frmProductReg" runat="server" class="frmProductReg" action="/Home/ProductAdded">
        <asp:Panel ID="BreadCrumbPanel" runat="server" HorizontalAlign="Left" CssClass="breadcrumbCentered">
            Register Your Product</asp:Panel>
        <asp:Panel ID="MainContentPanel" runat="server"
            HorizontalAlign="Left" CssClass="headerCentered">
            <!-- Grab the country name via the geo locator -->
            <h1>Two-Step Online Product Registration</h1>
            we detect that you&#39;re located in 
            <!-- using span as it can auto resize and keeping the input for form submission later -->
            <span id="geoLocatedCountry" class="selectedCountry" ><%= Html.Encode(ViewData["CountryName"]) %></span>
            To change your country or region, please
            <span id="changeCountryUpper">click here</span>
            .
            <br /><br />
            <h3>Step 1: Enter your product information</h3>
        </asp:Panel>
        <asp:Panel ID="Panel3" runat="server" CssClass="contentCentered" HorizontalAlign="Left">

            <h3 align="left">Method 1</h3>
            Key in the name of your product in the space below. Choose the most appropriate 
            if there are a few similar ones, then click
            <img id="Image1" src="Images/btn_addButtonS.gif" alt="Add Button"/>
            <br />
            You can register multiple products by repeating this step.
            <table id="Table1">
                <tr>
                    <td>
                    <input type="text" id="tboProductCount" class="tboProductCount" value="0" />
                    <input type="text" id="tboAddProductId" class="tboAddProductId" />
                    <input id="tboAddProduct" class="tboAddProduct" />
                    </td>
                    <td>
                    <input type="image" id="btnAddProduct" class="btnAddProduct" src="Images/btn_addButton.gif" />
                    </td>
                </tr>
            </table>
            <br />
            <h3 align="left">
                Method 2</h3>
            Select the product(s) you wish to register and click
            <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/btn_addButtonS.gif" />. 
            You can register multiple products by repeating this step. To remove
            <br />
            any product, click on the Remove button<asp:Image ID="Image3" runat="server" ImageUrl="~/Images/btn_remove.gif" />
            .<br />
            <br />
            <table border="0" cellpadding="2" cellspacing="0" frame="void" 
                style="width: 100%">
                <tr>
                    <td style="width: 238px">
                        <asp:ListBox ID="ListBox1" CssClass="LBox1" runat="server" Width="100%"></asp:ListBox>
                    </td>
                    <td style="width: 239px">
                        <asp:ListBox ID="ListBox2" CssClass="LBox2" runat="server" Width="100%"></asp:ListBox>
                    </td>
                    <td style="width: 290px">
                        <asp:ListBox ID="ListBox3" CssClass="LBox3" runat="server" Width="100%"></asp:ListBox>
                    </td>
                </tr>
                <tr>
                    <td style="width: 238px">
                        &nbsp;</td>
                    <td style="width: 239px">
                        &nbsp;</td>
                    <td style="width: 290px">
                        <asp:ImageButton ID="btnAddProduct2" cssClass="btnAddProduct2" runat="server" Height="26px" 
                            ImageAlign="Right" ImageUrl="~/Images/btn_addButton.gif" />
                    </td>
                </tr>
            </table>
            <br />
       </asp:Panel>
       <asp:Panel ID="PanelProductReg" runat="server" CssClass="PanelProductReg" HorizontalAlign="Left">
            <table id="ProductRegistrationTable" class="ProductRegistrationTable">
                <tr style="font-weight:bold;">
                    <td class="productIdCol">Product Id</td>
                    <td style="width:30%">Product</td>
                    <td style="width:30%">Serial Number<span class="redWarning">*</span> (
                    <a id="anchorSerialNoHelp" href="http://www.yahoo.com">What's this</a>)</td>
                    <td style="width:5%"><!-- used for the serial number help image button--></td>
                    <td style="width:20%">Date of Purchase<span class="redWarning">*</span></td>
                    <td>Remove</td>
                </tr>
            </table>
        </asp:Panel>
        <asp:Panel ID="Panel4" runat="server" HorizontalAlign="Left" CssClass="headerCentered">
            <h3>Step 2: Enter your personal details</h3>          
        </asp:Panel>
        <asp:Panel ID="Panel1" runat="server" CssClass="contentCentered" HorizontalAlign="Left">
            <table style="width: 97%">
                <tr>
                    <td style="width: 243px">
                        First Name<span class="redWarning">*</span>:</td>
                    <td>
                        <%= Html.TextBox("Customer.FirstName") %>
                        <!--asp:TextBox ID="customer.FirstName" ></asp:TextBox-->
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px; height: 18px">
                        Last Name<span class="redWarning">*</span>:</td>
                    <td style="height: 18px">
                        <%= Html.TextBox("Customer.LastName") %>
                        <!-- asp:TextBox ID="customer.LastName" ></asp:TextBox -->
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        Email Address<span class="redWarning">*</span>:</td>
                    <td>
                        <%= Html.TextBox("Customer.Email") %>
                        <!--asp:TextBox ID="customer.Email" ></asp:TextBox-->
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        Re-enter Email Address<span class="redWarning">*</span>:</td>
                    <td>
                        <input type="text" id="tboConfirmEmail" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        Country/Region<span class="redWarning">*</span>:</td>
                    <td>
                        <!-- hiding the input as i can't auto resize it and i still need this
                        data when i submit the form -->
                        <span id="selectedCountry" class="selectedCountry" ><%= ViewData["CountryName"] %>
                        </span>
                        <input type="hidden" id="customer.CountryID" value="65"/>
                        <input type="hidden" id="txtSelectedCountry" value="<%= Html.Encode(ViewData["CountryName"]) %>" />
                        <span id="currentCountry"> (Change this?)
                        </span>
                        <span id="changeCountry"> 
                        <%= Html.DropDownList("countries")%>
                        </span>
                    </td>
                </tr>
                <tr id="stateNameRow">
                    <td style="width: 243px">
                        State/Province:</td>
                    <td>
                        <!--%= Html.DropDownList("stateDdl") %-->
                        <asp:DropDownList ID="stateDdl" runat="server" CssClass="stateDdl"></asp:DropDownList>
                        &nbsp;&nbsp;&nbsp;&nbsp; or&nbsp;&nbsp;
                        <%= Html.TextBox("Customer.StateName")%>
                        <!--asp:TextBox ID="customer.StateName"></asp:TextBox-->
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        Gender:</td>
                    <td>
                        <%= Html.RadioButton("Customer.Male", "Male",true)%> Male
                        <%= Html.RadioButton("Customer.Female", "Female")%> Female
                        <!--asp:RadioButton ID="rbtMale" Text="Male" Checked="True" 
                            GroupName="rboGender" />
                        &nbsp;&nbsp;&nbsp;
                        <asp:RadioButton ID="rbtFemale"  Text="Female" 
                            GroupName="rboGender" /-->
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        Date of Birth:</td>
                    <td>
                        <input type="text" id="Customer_DOB" name="Customer.DOB" class="dobPicker" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        &nbsp;</td>
                    <td>
                        <%= Html.CheckBox("Customer.Subscribe",true) %>
                        <!--asp:CheckBox ID="cboNewsletter" Checked="True" / -->
                        &nbsp;I would like to receive newsletters for the latest Creative products, special 
                        offers and events.
                    </td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        &nbsp;</td>
                    <td>
                        * Denotes mandatory field.</td>
                </tr>
                <tr>
                    <td style="width: 243px">
                        &nbsp;</td>
                    <td>
                        <!--asp:ImageButton ID="ImageButton1"
                            ImageUrl="~/Images/btn_submit.gif" /-->
                        <input type="image" id="imgbtnSubmit" src="Images/btn_submit.gif"/>    
                        <input type="image" id="imgbtnClear" src="Images/btn_clear.gif"/>
                    </td>
                </tr>
            </table>

        </asp:Panel>
    </form>
</asp:Content>

3 个答案:

答案 0 :(得分:1)

重定向后 - 获取模式是您问题的答案。搜索SO或Google,你应该找到很多信息。

你还提到ASP.NET控件吗?那些不应出现在ASP.NET MVC View页面中,因为它们与MVC不兼容。

答案 1 :(得分:1)

在处理请求的方法中,您可以返回Redirect(url);这应该可以解决您当前的问题。

你应该试图远离的另一件事是使用asp.net控件。你在MVC中不需要它们。如果您确实使用它们,请不要将它们与viewstate一起使用,因为MVC不会对您的Form元素执行任何操作,因为Webforms会使ViewState工作。

答案 2 :(得分:1)

简而言之,您不打算使用在MVC中使用viewstate的ASP.NET服务器控件 - ASP.NET MVC不支持viewstate。依赖于viewstate的任何控件都不起作用 - 甚至那些不符合MVC哲学的控件也是如此。