我有一个需要以下功能的场景:
In View I have call as:
$.ajax({
type: "POST",
async: false,
dataType: 'json',
url: "ControllerA/ActionA",
data: { var1: some_value },
success: function (data) {
if (data == true) {
form.submit();
}
else if (data == false) {
}
});
// In ControllerA
public JsonResult ActionA(string var1)
{
/*
Some manipulation and calculations
*/
_slist = RedirectToAction("ActionC", "ControllerB", new { var1 = some_value});
string = _slist.First().ToString();
return RedirectToAction("ActionB", "ControllerB", new { var1 = var2 });
}
// In ControllerB
public JsonResult ActionB(string var1)
{
/*
Some manipulation and calculations
*/
return Json(false, JsonRequestBehavior.AllowGet);
}
public SelectList ActionC(string var1)
{
/*
Some manipulation and calculations
*/
Session["STRING"] = some_value;
return new SelectList(_storeOrderTimeDictionaryList, "Value", "Key");
}
我在View页面中需要JsonResult,但问题是:
答案 0 :(得分:1)
这可能不是最好的方法......
很难说,但是干掉控制器,移出业务逻辑可能有所帮助。看起来您想要维护操作B和C的功能。
$.ajax({
type: "POST",
async: false,
dataType: 'json',
url: "ControllerA/ActionA",
data: { var1: some_value },
success: function (data) {
if (data == true) {
form.submit();
}
else if (data == false) {
}
});
public Class CalculationsA
{
public void DoCalculation() {}
}
public Class CalculationsB
{
public void DoCalculation() {}
}
public Class CalculationsC
{
public IQueryable<somethign> DoCalculation() {}
}
//_a is declared in Controller A as CalculationsA
//_b is declared in Controller B as CalculationsB
//_c is declared in Controller C as CalculationsC
// In ControllerA
public JsonResult ActionA(string var1)
{
_a.DoCalculation();
_slist = _b.DoCalculation().First().ToString();
Session["STRING"] = some_value;
_c.DoCalculation();
/* your other logic... */
return Json(retval, JsonRequestBehavior.AllowGet);
}
// In ControllerB
public JsonResult ActionB(string var1)
{
_b.DoCalculation();
return Json(false, JsonRequestBehavior.AllowGet);
}
public SelectList ActionC(string var1)
{
_c.DoCalculation();
Session["STRING"] = some_value;
return new SelectList(_storeOrderTimeDictionaryList, "Value", "Key");
}
顺便说一下,您应该查看Ninject,Castle Windsor,结构图或任何其他DI / IOC容器,以帮助您测试此逻辑(并使其干燥)。尝试搜索ninject asp.net mvc 2教程
答案 1 :(得分:0)
您是否可以不重构控制器操作以将Some manipulation and calculations
提取到另一个类或服务层函数调用中。
由于我需要ActionC中的Session,我无法实例化Controller并调用该动作。
没有什么可以阻止您在ControllerA.ActionA
中使用会话。以下内容并不准确,但可以帮助您..
public class ControllerA{
public JsonResult ActionA(string var1)
{
/* Some manipulation and calculations */
SomeService service = new SomeService();
_slist = service.ActionThatDoesStuffForActionC(var1);
Session["STRING"] = var1;
var firstItem = _slist.First().ToString();
SomeOtherService service2 = new SomeOtherService();
var service2Result = service2.ActionThatDoesStuffForActionB(firstItem);
// convert service2Result to a jsonresult here.
return RedirectToAction("ActionB", "ControllerB", new { var1 = firstItem });
}
}
public class ControllerB{
public JsonResult ActionB(string var1)
{
/* Some manipulation and calculations */
SomeOtherService service2 = new SomeOtherService();
var service2Result = service2.ActionThatDoesStuffForActionB(var1);
return Json(false, JsonRequestBehavior.AllowGet);
}
public SelectList ActionC(string var1)
{
/* Some manipulation and calculations */
SomeService service = new SomeService();
_slist = service.ActionThatDoesStuffInActionC(var1);
Session["STRING"] = var1;
return new SelectList(_slist, "Value", "Key");
}
}
编辑:查看此处http://www.lostechies.com/blogs/jimmy_bogard/archive/2010/07/23/mvcconf-slides-and-code-posted.aspx的源代码。我认为Jimmy Boggard的方法可能很有用,并为您提供了一种调用“其他控制器”操作的方法。你评论'我无法改变行动的行为。重构它需要时间,我没有。对我而言,这标志着通往不可维护代码的道路。重构,重构,重构 - 现在这样做的好处将在以后的阶段为您节省数小时的心痛。基于这个问题,我认为它已经开始了。