我已经搜索了相当长一段时间关于如何将纯 html 链接到 mvc 4 Controller ,但结果是空的。我不确定我做错了什么,尽管我知道我的代码中会有很多错误,因为我是新手。
我有一个简单的网页,我试图链接到 Controller ,但当我点击提交时,我收到错误。我还使用 Bootstrap(本例中并未真正使用,但链接在那里),但我确信这不是此错误的根源,而是服务器端的。我收到的错误是
0x800a1391 - JavaScript runtime error: 'Button1_Click' is undefined
HTML 页面实际上是一个 aspx,因为我想从 java 轻松转换小型网站。
这里是示例页面(它已被编辑多次,并使用不同的提交按钮变体):
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>arrggghh</title>
<meta name="generator" content="Bootply" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="/css/bootstrap.css" rel="stylesheet">
<link href="/css/styles.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<div class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Page</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="index.html">Home</a></li>
</ul>
<ul class="nav navbar-nav navbar-right"></ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div id="header">
</div>
<div class="content-form-blank2">
<div>
<table class="margin">
<tr>
<td><h4>Press button.</h4></td>
</tr>
</table>
</div>
<form id="myform" action="Home" method="post">
<input type="submit" onclick="Button1_Click" />
</form>
</div>
</div>
<div class="form-navigation-bottom">
</div>
<!-- /.container -->
<!-- script references -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
这是我的示例 Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using arrgg.code;
namespace arrgg.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
protected void Button1_Click()
{
new arrggOut();
}
}
}
这个错误并不像我理解如何将纯 html 链接到 Controller 那么重要。预先感谢...如果有人有必要的时间写一本关于我做错的一切的书。
我很快就会开始使用帮助程序并阅读有关 ASP.NET 和 MVC 的更多信息
最佳答案
您需要从表单提交中发布到正确的路线,例如/主页/做某事。然后,在您的 Controller 中,如果已建立路由,您将被映射到 Home Controller 中的 DoSomething 方法。不要将其写为Button1_Click()。当您单击该按钮时,它应该简单地提交带有适当 URL 的表单作为其操作,除非您需要在提交之前执行某些操作,例如客户端验证。然后, Controller 可以处理它并用它做一些有意义的事情,如方法名称所描述的。
查看:
<form id="myform" action="/Home/DoSomething" method="post">
<input type="submit" />
</form>
Controller :
namespace arrgg.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult DoSomething()
{
// Or whatever you require here...
return View();
}
}
}
更多信息:http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-an-action-cs
关于c# - 如何在 C# 中将纯 html 表单链接到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36408427/