0

ASP.NET MVC は初めてです。アクション メソッドではないコントローラのメソッドをビュー スルーで呼び出したいのですが$.ajax、ブラウザ コンソールでエラーが発生します。アクションメソッドではないメソッドをajaxで呼び出すことはできないのでしょうか。

ビューのコード

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

    function ajaxcall() {
        $.ajax({
            url: '/Home/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>

メソッドのコードは次のとおりです

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";
        }
    }
}
4

1 に答える 1

0

NonAction属性でマークされていない限り、コントローラーのすべてのパブリック メソッドはアクションです。

この[WebMethod]属性は ASP.NET Web サービス (非 MVC) から取得され、MVC コントローラーでは効果がありません。

ValidatePinメソッドは静的であるため、呼び出されません。アクション メソッドはインスタンス メソッドである必要があります。

于 2013-01-05T21:12:24.870 に答える