通过javascript调用控制器的方法

时间:2022-12-02 10:23:01

hello all I am new to mvc I want to call a method of a controller which is not an action method in a view through $.ajax but i am getting error in browser console .I want to ask that is it not possible to call a method which is not action method through ajax. code for view

你好我所有的mvc新手我想调用一个控制器的方法,它不是通过$ .ajax在视图中的动作方法但我在浏览器控制台中收到错误。我想问这是不可能调用一个通过ajax不是动作方法的方法。视图代码


@{
    ViewBag.Title = "About Us";
}
<script type="text/javascript">



    function ajaxcall() {
        $.ajax({
            url: 'ValidatePin',
            type: 'Post',
            success: function (result) {

                alert(result.d);

            }


        })
    }
</script>
<h2>About</h2>
<p>
     Put content here.

<input type="button" onclick="ajaxcall()"  value="clickme" />


</p>

here is my code for method

这是我的方法代码

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
   [WebMethod]
        public static string ValidatePin()
        {
            return "returned from controller class";

        }

    }
}

2 个解决方案

#1


0  

why do you insist not to implement an action method instead of this static method? my second question is what your method actully does? validation something? so how do you pass it to this method?

为什么你坚持不实现一个动作方法而不是这个静态方法?我的第二个问题是你的方法究竟做了什么?验证什么?那你怎么把它传递给这个方法呢?

Think you might want something like this:

认为你可能想要这样的东西:

public JsonResult ValidatePin(string id) /*id=pin*/
{
    bool result;
    // do something
    return Json(new { IsValid = result, Message = "something" }, JsonRequestBehavior.AllowGet);
}


$.ajax({
                url: '/validatepin/' + pin,
                type: 'POST',
                dataType: 'json',
                data: json,
                contentType: 'application/json; charset=utf-8',
                success: function (data) {

        alert(data.message + '  ' + data.IsValid);
                }
            })

#2


1  

Alternative solution with incoding framework:

带有编码框架的替代解决方案:

Controller

调节器

        [HttpGet]
    public ActionResult ValidatePin()
    {
        return IncJson(new AlertVM(){Message = "Some thing"});
    }

Razor page

剃刀页面

@(Html
  .When(JqueryBind.Click)
  .Do()
  .AjaxGet(Url.Action("ValidatePin","Pin"))
  .OnSuccess(dsl => dsl.Utilities.Window.Alert(Selector.Incoding.Data<AlertVM>(r=>r.Message)))
  .AsHtmlAttributes()
  .ToButton("clickme"))

#1


0  

why do you insist not to implement an action method instead of this static method? my second question is what your method actully does? validation something? so how do you pass it to this method?

为什么你坚持不实现一个动作方法而不是这个静态方法?我的第二个问题是你的方法究竟做了什么?验证什么?那你怎么把它传递给这个方法呢?

Think you might want something like this:

认为你可能想要这样的东西:

public JsonResult ValidatePin(string id) /*id=pin*/
{
    bool result;
    // do something
    return Json(new { IsValid = result, Message = "something" }, JsonRequestBehavior.AllowGet);
}


$.ajax({
                url: '/validatepin/' + pin,
                type: 'POST',
                dataType: 'json',
                data: json,
                contentType: 'application/json; charset=utf-8',
                success: function (data) {

        alert(data.message + '  ' + data.IsValid);
                }
            })

#2


1  

Alternative solution with incoding framework:

带有编码框架的替代解决方案:

Controller

调节器

        [HttpGet]
    public ActionResult ValidatePin()
    {
        return IncJson(new AlertVM(){Message = "Some thing"});
    }

Razor page

剃刀页面

@(Html
  .When(JqueryBind.Click)
  .Do()
  .AjaxGet(Url.Action("ValidatePin","Pin"))
  .OnSuccess(dsl => dsl.Utilities.Window.Alert(Selector.Incoding.Data<AlertVM>(r=>r.Message)))
  .AsHtmlAttributes()
  .ToButton("clickme"))