如何在MVC C#中从另一个Action方法调用一个Action方法(两者都在同一个Controller中)?

时间:2015-08-18 12:42:36

标签: c# asp.net-mvc model-view-controller

嗨,从调用Action方法,我有:

[HttpPost]
public JsonResult SubmitForms(Note note, string action = "Submit") 
{
     //some code
     RedirectToAction("SaveDemographicForm", "PatientForms", new { model.DemographicFormData,  action="Submit" , submitAll = true });
     //some code
}

这就是我想要调用的Action方法:

[HttpPost]
public JsonResult SaveDemographicForm(DemographicForm demographicForm, string action = "Save", bool submitAll = false )
{
      //Some code
}

我在这里做错了什么? 提前谢谢。

4 个答案:

答案 0 :(得分:3)

如果他们都在同一个控制器中,您不需要重定向到操作,只需直接调用它。

[HttpPost]
public JsonResult SubmitForms(Note note, string action = "Submit") 
{
     //some code
     return SaveDemographicForm(new DemographicForm { /*your properties*/ }, "Save", false);
     //some code
}

[HttpPost]
public JsonResult SaveDemographicForm(DemographicForm demographicForm, string action = "Save", bool submitAll = false )
{
      //return some json object
}

答案 1 :(得分:1)

只需拨打电话

[HttpPost]
public JsonResult SubmitForms(Note note, string action = "Submit") 
{
     SaveDemographicForm(new DemographicForm { /*your properties*/ }, "Save", false);

     //somecode
     //submitforms return
}

[HttpPost]
public JsonResult SaveDemographicForm(DemographicForm demographicForm, string action = "Save", bool submitAll = false )
{
      //return some json object
}

答案 2 :(得分:1)

  • 使用System.Web.Mvc;
  • 使用System.Web.Mvc.Html;

    public ActionResult Index()
    {
        HtmlHelper helper = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(ControllerContext, "Index"), new ViewDataDictionary(), new TempDataDictionary(), new System.IO.StringWriter()), new ViewPage());
        helper.RenderAction("Index2");//call your action
    
        return View();
    }
    
    public ActionResult Index2(/*your arg*/)
    {
        //your code
        return new EmptyResult();
    }
    

答案 3 :(得分:0)

RedirectToAction()返回RedirectToRouteResult

public ActionResult AnotherAction(int id)
{
    //Do something with the id ...
    return View()
}


public RedirectToRouteResult DoAndRedirect()
{            
    //Do something and go to the desired view:
    return RedirectToAction("AnotherAction", new { id = x.ID });
}