如何在 C# 中将纯 html 表单链接到控制器 [英] How do I link pure html form to controller in C#

查看:17
本文介绍了如何在 C# 中将纯 html 表单链接到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何将纯 html 链接到 mvc 4 控制器的问题,我已经搜索了很长时间,但结果是空的.我不确定我做错了什么,但我知道我的代码中会有很多错误,因为我是新手.

我有一个简单的网页,我试图将其链接到控制器,但每次点击提交时都会出错.我也在使用 Bootstrap(在这个例子中没有真正使用,但链接在那里),但我确定这不是这个错误的根源,它是服务器端.我得到的错误是

0x800a1391 - JavaScript 运行时错误:'Button1_Click' 未定义

HTML 页面实际上是一个 aspx,因为我想从 java 中轻松转换小型网站.

这是示例页面(已多次编辑,提交不同的按钮变体):

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %><!DOCTYPE html><html lang="zh-cn"><头><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="样式表"><link href="/css/styles.css" rel="stylesheet"><!--[如果是 IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]--><身体><div id="包装器"><div class="navbar navbar-default navbar-static-top"><div class="容器"><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><a class="navbar-brand" href="index.html">页面</a>

<div class="collapse navbar-collapse"><ul class="nav navbar-nav"><li><a href="index.html">首页</a></li><ul class="nav navbar-nav navbar-right"></ul></div><!--/.nav-collapse -->

<div id="标题">

<div class="content-form-b​​lank2"><div><table class="margin"><tr><td><h4>按下按钮.</h4></td></tr>

<form id="myform" action="Home" method="post"><input type="submit" onclick="Button1_Click"/></表单>

<div class="form-navigation-bottom">

<!--/.container --><!-- 脚本引用--><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script><script src="/js/bootstrap.min.js"></script></html>

这是我的示例控制器:

使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;使用 arrgg.code;命名空间 arrgg.Controllers{公共类 HomeController : 控制器{公共 ActionResult 索引(){返回视图();}[HttpPost]受保护的无效 Button1_Click(){新的 arrggOut();}}}

错误并不像我理解如何将纯 html 链接到控制器那么重要.提前致谢...如果有人有时间写一本关于我做错的一切的书.

我将很快开始使用帮助程序并阅读有关 asp.net 和 MVC 的更多信息

解决方案

你需要从你的表单提交到正确的路径,例如/家/做点什么.然后,在您的控制器中,如果路由已经建立,您将被映射到 Home 控制器中的 DoSomething 方法.不要将其写为 Button1_Click().当您单击该按钮时,它应该简单地提交带有相应 URL 的表单作为其操作,除非您需要在提交之前执行某些操作,例如客户端验证.然后,控制器可以处理它并用它做一些有意义的事情,由方法名称描述.

查看:

<输入类型=提交"/></表单>

控制器:

命名空间 arrgg.Controllers{公共类 HomeController : 控制器{公共 ActionResult 索引(){返回视图();}[HttpPost]公共 ActionResult DoSomething(){//或者你需要的任何东西...返回视图();}}}

更多信息:http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-an-action-cs

I have searched for quite some time on how to link pure html to mvc 4 controllers and came up empty. I am unsure of what I'm doing wrong, though I know there will be many errors in my code since I'm new to this.

I have a simple web page that I'm trying to link to a controller but I'm getting an error when ever I hit submit. I'm also using Bootstrap (not really used in this example but the links are there), but I'm sure that's not the source of this error and that it's server side. The error I'm getting is

0x800a1391 - JavaScript runtime error: 'Button1_Click' is undefined

The HTML page is actually an aspx because I wanted to do easy conversions of small websites over from java.

Here is the example page (It's been edited many times with different variations of buttons to submit):

<%@ 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>

And this is my example 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();
        }
    }
}

The error isn't as important as me understanding how to link pure html to the controller. Thanks in advance... if anyone has the required time to write a book about everything I'm doing wrong.

I will soon start using helpers and reading up more about asp.net and MVC

解决方案

You need to post from your form submission to the correct route e.g. /Home/DoSomething. Then, in your controller, provided the route has been established, you will be mapped through to the DoSomething method in your Home controller. Do not write it as Button1_Click(). When you click that button, it should simply submit the form with the appropriate URL as its action, unless you need to do something prior to submission e.g. client side validation. Then, the controller can handle it and do something meaningful with it, described by the method name.

View:

<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();
        }
    }
}

Further info: http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/creating-an-action-cs

这篇关于如何在 C# 中将纯 html 表单链接到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆