【问题标题】:Getting exception for <%=@Html.DropDownList%> {"Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server."}<%=@Html.DropDownList%> {"Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server."} <%=@Html.DropDownList%>
【发布时间】:2014-06-19 01:44:36
【问题描述】:

该项目位于 .Net Framnework 3.5 上,并具有 MVC 框架 1.0 ViewModel 是:

namespace MockBDPWorkflowTestApp.ViewModels


{
    public class WorkFlowTestViewModel
   {
    public string processInstance { get; set; }
    public string mail { get; set; }
    public String[,] productCode = { { "GL", "0" }, { "PROP", "1" }, 
                       { "Auto", "2" }, { "Other", "3"},
                       { "Multi", "4" } };

    public List<SelectListItem> products;
    }
}

控制器是:

 public class WorkFlowTestController : Controller
{
    //
    // GET: /WorkFlowTest/

    public ActionResult OpenSubmission(string processId, string mailId)
    {
        var SubmissionModelView = new MockBDPWorkflowTestApp.ViewModels.WorkFlowTestViewModel{processInstance =processId,  mail =mailId} ;
        SubmissionModelView.products = new List<SelectListItem>();
        SubmissionModelView.products.Add(new SelectListItem { Text = "GL", Value = "0", Selected = true });
        SubmissionModelView.products.Add(new SelectListItem { Text = "Property", Value = "1" });
        SubmissionModelView.products.Add(new SelectListItem { Text = "Package", Value = "2" });
        SubmissionModelView.products.Add(new SelectListItem { Text = "Island Marine", Value = "3" });
        return View("Submission", SubmissionModelView);

    }

}

观点是:

<%@ Page Language="C#"     Inherits="System.Web.Mvc.ViewPage<MockBDPWorkflowTestApp.ViewModels.WorkFlowTestViewModel>" %>
  <!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 id="Head1" runat="server">

<title>Submission</title>

 </head>

<body>

  <div>

    <% using (Html.BeginForm())
      {
        %>
        <div>
            <table >
             <tr>
              <td>
               <div id="SID">
                   <table id="tblID" cellpadding ="0" cellspacing ="0" border="1">
                    <tr>
                        <td width ="50%"> <label for="Process Inastance Id"> Process Inastance Id: </label> &nbsp;&nbsp; <%=@Html.TextBox("pInstance",@Model.processInstance) %></td>
                        <td width ="50%"> <label for ="Mail Id">Mail Id: </label> &nbsp;&nbsp; <%=@Html.TextBox("mail",@Model.mail) %> </td>
                    </tr>
                </table>
            </div>
            <br /><br />
            <table id="tblDet" cellpadding ="0" cellspacing ="0" >
                <tr>
                    <td width="50%"> <label for="Submission Number"> Submission Number: </label> &nbsp;&nbsp; <%=@Html.TextBox("sNo") %></td>
                    <td width ="50%"> <label for ="Name Insured">Name Insured: </label> &nbsp;&nbsp; <%=@Html.TextBox("nameInsured") %> </td>
                </tr>
                <tr>
                    <td width="50%"> <label for="Broker Code"> Broker Code: </label> &nbsp;&nbsp; <%=@Html.TextBox("brokerCode") %></td>
                    <td width ="50%"> <label for ="Broker Name">Broker Name: </label> &nbsp;&nbsp; <%=@Html.TextBox("brokerName") %> </td>
                </tr>
                 <tr>
                    <td width="50%"> <label for="Product Code"> Product Code: </label> &nbsp;&nbsp; <%=@Html.DropDownList("prodlist",@Model.products)%></td>
                    <td width ="50%"> <asp:Button ID="Button1" runat ="server"  Text ="Add Product" /> </td>
                </tr>
            </table>
         </td>
        </tr>
       </table>
    </div>

<% } %>
</div>

在视图中我找到 HTMLhelper 下拉列表,但在运行时它给出了一个异常 {"Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server. "}

在 MVC Framework 1.0 中,制作列表项下拉框的最佳方式是什么。

【问题讨论】:

  • 您正在混合使用两种不同的 Microsoft 技术。 &lt;asp:Button /&gt; 是网络表单,在 ASP.NET MVC 视图中无法正常工作。
  • 您正在混合使用 asp.net mvc 和 asp.net 标记。这不受支持,至少不应该发生。对于按钮,您应该只有 &lt;input type="button" ... /&gt;

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


【解决方案1】:

用纯 HTML &lt;button&gt; 或 &lt;input type="submit" /&gt; 元素替换您的 &lt;asp:Button ID="Button1" runat="server" /&gt;。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-29
    • 2022-12-17
    • 2022-12-22
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多