ASP.NET MVC 4 Controller + Partial View – 判斷 AJAX 請求

 
ASP.NET MVC 4 Controller + Partial View – 判斷 AJAX 請求
 

   Controller
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
 
   namespace MvcApplication.Controllers
   {
      public class HomeController : Controller
      {
 
         BITestEntities entity;
 
         public ActionResult ProductItem(int? id)
         {
 
            if (Request.IsAjaxRequest()) {
               entity = new BITestEntities();
 
               if (id.HasValue)
               {
                  var DataModel = entity.ProductEntities.Where(r => r.id == id).FirstOrDefault();
                  if (DataModel == null) DataModel = this.getDefaultNullValue();
                  return PartialView(DataModel);
               }
               else
               {
                  var DataModel = entity.ProductEntities.Where(r => r.id == 0).FirstOrDefault();
                  if (DataModel == null) DataModel = this.getDefaultNullValue();
                  return PartialView(DataModel);
               }
            }else{
               return RedirectToAction("Index");
            }
 
         }
 
         … …
 
      }
   }