Reputation: 1331
I can't figure out what i'm doing wrong. My project launches with an index view which posts to fileUpload action. From fileUpload action another method in the controller is called changeText() and at the end of changeText() there is a call to Results(text) which takes the parameter string. At the end of Results() there is the line return View() to Results.cshtml. But Results.cshtml doesn't get loaded. Just wondering if i'm missing something.
public ActionResult Index()
{
return View();
}
[HttpPost]
public void FileUpload(HttpPostedFileBase file)
{
Debug.WriteLine(file.FileName)
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
file.SaveAs(pathName);
}
changeText(txt);
}
public void changeText (string text)
{
ResultX(textChange);
}
public ActionResult ResultX(string text)
{
Debug.WriteLine("resultx action");
return View(text);
}
Thanks.
Upvotes: 0
Views: 65
Reputation: 872
I can't see the reason for all those methods! your code can be very simple.
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
Debug.WriteLine(file.FileName)
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
file.SaveAs(pathName);
}
Debug.WriteLine("resultx action");
return View("ResultX",text);
}
if all of this not working make sure that you're calling the right view
Upvotes: 1
Reputation: 239260
It doesn't seem like you actually understand how actions work. First, the return type of an action determines what response is sent to the client. Returning void
means simply that you're going to return an empty response body with a 200 status code. In terms of a user accessing your site via a web browser, that translates to a completely blank page, which is not what you want.
What you want, here, is to return a ViewResult
utilizing ResultX.cshtml
. That means your FileUpload
action needs to return ViewResult
or more generally ActionResult
. ActionResult
is the base class for all other result types, so if you type your return that way, you can return any kind of result you like, whether that be ViewResult
, RedirectResult
, EmptyResult
, etc.
Given that, the existence of changeText
and ResultX
as methods are superfluous. There's zero reason to pass this around between three different methods that all pretty much do the same thing. Just do return View("ResultX", txt);
directly in FileUpload
. However, a successful post should follow the PRG (Post-Redirect-Get) pattern, so the form doesn't get resubmitted if the user refreshes the page. As a result, you should really be returning a redirect on success and returning the original FileUpload
view on error. Altogether, that would look like:
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
if (file != null)
{
// Moved this inside the conditional because you can't
// reference `file.FileName` unless `file` is non-null
Debug.WriteLine(file.FileName);
var fileName = Path.GetFileName(file.FileName);
pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
file.SaveAs(pathName);
return RedirectToAction("Index");
}
// Traditionally, you'd pass in what was posted to the call to `View`,
// but you cannot repopulate a file input, so there's no point here.
return View();
}
Upvotes: 1
Reputation: 2361
This is what your code should be:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file)
{
Debug.WriteLine(file.FileName)
if (file != null)
{
var fileName = Path.GetFileName(file.FileName);
pathName = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
file.SaveAs(pathName);
}
return changeText(txt);
}
public ActionResult changeText (string text)
{
return ResultX(textChange);
}
public ActionResult ResultX(string text)
{
return View("ResultX", text);
}
Upvotes: 2