操作方法选择器使用

/*************************ActionMethodController.cs************************************/

using System;
using System.Web.Mvc;
using Chapter15.Areas.ActionMethodSelector.Utility;

namespace Chapter15.Areas.ActionMethodSelector.Controllers
{
    public class ActionMethodController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [AjaxOnly]
        public ActionResult Index(FormCollection unused)
        {
            return Content(String.Format("<p>" + DateTime.Now + "</p>"));
        }
    }
}
/************************************AjaxOnlyAttribute .cs********************************************/

using System.Reflection;
using System.Web.Mvc;

namespace Chapter15.Areas.ActionMethodSelector.Utility
{
    public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
    {
        public override bool IsValidForRequest(ControllerContext controllerContext,
                                               MethodInfo methodInfo)
        {
            return controllerContext.HttpContext.Request.IsAjaxRequest();
        }
    }
}

/**************************************Index.cshtml***********************************************/

@{ ViewBag.Title = "Action Method Selector Sample"; }

<h3>Dates go here for each submit...</h3>
<div id="dates"></div>

@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "dates" }))
{
    <input type="submit" />
}
 

发布了488 篇原创文章 · 获赞 73 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/104496105
今日推荐