0

大家好,我是 mvc 新手不是通过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>

这是我的方法代码

   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

2 回答 2

1

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

控制器

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

剃刀页面

@(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"))
于 2013-05-06T06:18:27.610 回答
0

为什么你坚持不实现一个动作方法而不是这个静态方法?我的第二个问题是你的方法实际上是做什么的?验证什么?那么你如何将它传递给这个方法呢?

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

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);
                }
            })
于 2013-01-05T21:39:21.723 回答