Asp.net MVC 按钮不显示

Asp.net MVC button does not show

我在 div 标签中放置了一个按钮,当我 运行 它时,我只看到 div 容器

<div style= "border-style:solid;border-width:1px; padding:5px; background-color:#ffffcc">
    <asp:button ID = "refresh" runat ="server" OnClientClick="reloadPage()">
    <script type="text/javascript">
    function reloadPage(){
        window.location.reload()    
    }
    </script>
</asp:button>
</div>

refresh 按钮无处可寻。我的代码有什么问题吗?我是MVC新手,请多多指教

为什么要在 MVC 项目中使用 asp 控件 -> 使用 HTML 元素。

使用表格

<div>
    @using (Html.BeginForm("Action", "Controller", FormMethod.Post))
    {
        <button type="submit">Submit</button>
    }
</div>

使用按钮

<div>
    <button onclick="DoSomeJavascript" >Submit</button>
</div>

MVC 使用控制器(代码隐藏)和视图 (Html) 来创建页面。要向页面发送数据或从页面发送数据,您需要使用视图模型。

也许看看这个方便的指南:http://www.asp.net/mvc/overview/getting-started/introduction/getting-started

您的问题表明您是 MVC 的新手,但您正试图从经典 ASP.NET 中定义一个按钮,MVC 使用的 Razor 视图引擎将不会对其进行处理。由于您只是重新加载页面,您可以使用常规 HTML 按钮和 Javascript 来完成此操作。我不会在此片段中重新加载页面,但这无关紧要。

<div style="border-style:solid; border-width:1px; padding: 5px; background-color:#ffffcc">
  <button id="refresh" onclick="reloadPage()">
    Refresh Page
    <script type="text/javascript">
     function reloadPage() {
       console.log("Reloaded")
       //Reload page here
      }
    </script>
  </button>
</div>

如果您 运行 单击此按钮,您应该会在 Javascript 控制台中看到日志消息。