Is it possible to return anything else from the controller other than the view?

Is it possible to return a string from the controller when the form is submitted?

+2


a source to share


3 answers


You can, as Darin suggests, return Content (string); There are other possibilities such as

[HttpPost]
public ActionResult Index(FormCollection form) {
    /*
      return Json(json);
      return View;
      return PartialView;
    */
}

      



If you return anything other than the result of the action, it will be automatically completed in the ContentResult.

[HttpPost]
public ContentResult Index(FormCollection form) {
    /*
      return Content(string); 
      return File(bytes, contentType);
      return DateTime.Now;
      return 2;
    */
}

      

+3


a source


[HttpPost]
public ActionResult Index()
{
    return Content("some string", "text/plain");
}

      



+6


a source


public ActionResult Index()
{
    // ...
    return File(bytes, contentType);
}

      

+3


a source







All Articles