[英]A controller method that calls a different method on the same controller
I have a controller method: 我有一个控制器方法:
public ActionResult Details(int id)
{
Order order = OrderFacade.Instance.Load(id);
return View(order);
}
that is used for 95% of possible invocations. 用于95%的可能调用。 For the other 5% i need to manipulate the value of
id
before passing to the facade. 对于其他5%,我需要在传递到立面之前操纵
id
的值。 I'd like to create a separate method within this same controller that executes that manipulation and then calls this (Details) method. 我想在同一控制器内创建一个单独的方法,该方法执行该操作,然后调用此(详细信息)方法。
What would the signature of that method look like? 该方法的签名是什么样的? What is the syntax to call the main Details method?
调用主Details方法的语法是什么?
public ??? ManipulatorMethod(int id)
{
[stuff that manipulates id]
[syntax to call Details(manipulatedID)]
}
mny thx mny thx
public ActionResult ManipulatorMethod(int id)
{
//[stuff that manipulates id]
int _id = id++;
//[syntax to call Details(manipulatedID)]
return RedirectToAction("Details", new { id = _id });
}
//assuming that {id} exists on the route (usually in the default route) //假设{id}在路由上(通常在默认路由中)
If you will invoke the manipulator method directly as an action on the controller you can do this: 如果您将直接调用manipulator方法作为对控制器的操作,则可以执行以下操作:
public ActionResult ManipulatorMethod( int id )
{
// Do something to id
return Details( id );
}
If all access will be through the Details action then you can do this: 如果所有访问都将通过“详细信息”操作进行,则可以执行以下操作:
public ActionResult Details( int id )
{
if( IdNeedsManipulation( id ) )
id = ManipulateId( id );
return View( id );
}
private int ManipulateId( int id )
{
// Do something to id
return id;
}
private bool IdNeedsManipulation( int id ) { return ...; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.