One of the beauties of MVC is the ability to use Actions for both normal web calls as well as Ajax calls. But in doing so, I'm a little uncomfortable with my code. I'd like to know what other people think.
MVC的优点之一是能够同时对普通web调用和Ajax调用使用操作。但是这样做,我对我的代码有点不舒服。我想知道其他人是怎么想的。
So I want to view the details of an item in a list. I have created a Details view. I also have decided to use hijaxing -- if a user has Javascript enabled, I want the Details form to be a popup. So for the Details.aspx, I need full html, but if it's an ajax request, I only need the form elements. So here's how I wrote my controller method:
我想要查看列表中某项的细节。我创建了一个Details视图。我还决定使用hijaxing——如果用户启用了Javascript,我希望Details表单是一个弹出窗口。所以对于细节。aspx,我需要完整的html,但是如果是ajax请求,我只需要表单元素。我是这样写我的控制器方法的
public ActionResult Details(int id)
{
if (Request.IsAjaxRequest())
{
return PartialView(GetAjaxModel());
}
else
return View(GetModel());
}
It works, but whenever I have a giant "if" statement surrounding my entire code, it bothers me. How can I get rid of that and/or make the code better?
它可以工作,但是每当我在整个代码周围有一个巨大的“如果”语句时,它就会困扰我。我怎样才能摆脱它,或者使代码更好?
I could also write a separate method called AjaxDetails, but what I'd really like to write is this:
我也可以写一个单独的方法叫AjaxDetails,但是我真正想写的是
public ActionResult Details(int id)
{
return View(GetModel());
}
[Ajax]
public ActionResult Details(int id)
{
return View(GetAjaxModel());
}
But to my knowledge, there's no attribute to filter Ajax vs normal calls.
但据我所知,没有什么属性可以过滤Ajax和普通调用。
How do you write your Ajax calls?
如何编写Ajax调用?
UPDATE Clicktricity does indeed have the correct answer. One additional change, however is that that since 2 controller methods cannot have the same signature, I need to write the controller code as such:
更新Clicktricity确实有正确的答案。另外一个变化是,由于2个控制器方法不能有相同的签名,我需要这样写控制器代码:
public ActionResult Details(int id)
{
return View(GetModel());
}
[AjaxRequest]
[ActionName("Details")]
public ActionResult DetailsAjax(int id)
{
return PartialView(GetAjaxModel());
}
1 个解决方案
#1
3
You're correct that there isn't a filter that does that - but in a typical MVC fashion, there is no reason why you couldn't write your own.
您是对的,没有一个过滤器可以做到这一点——但是以典型的MVC方式,您没有理由不编写自己的过滤器。
public class AjaxRequestAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
return (controllerContext.HttpContext.Request.IsAjaxRequest())
}
}
Then you can use [AjaxRequest]
on your action methods.
然后可以在操作方法上使用[AjaxRequest]。
(Please note, I haven't tested this)
(请注意,我还没有测试过这个)
#1
3
You're correct that there isn't a filter that does that - but in a typical MVC fashion, there is no reason why you couldn't write your own.
您是对的,没有一个过滤器可以做到这一点——但是以典型的MVC方式,您没有理由不编写自己的过滤器。
public class AjaxRequestAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
return (controllerContext.HttpContext.Request.IsAjaxRequest())
}
}
Then you can use [AjaxRequest]
on your action methods.
然后可以在操作方法上使用[AjaxRequest]。
(Please note, I haven't tested this)
(请注意,我还没有测试过这个)