繁体   English   中英

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

[英]How to call one Action method from another Action method(both are in the same Controller) in MVC C#?

嗨,从调用操作方法中,我有:

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

这是我试图调用的操作方法:

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

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

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

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

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

只打电话不回

[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
}

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 });
}
  • 使用 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(); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM