Search code examples
c#asp.net-mvcpost-redirect-get

Display confirmation message after post with RedirectToAction and without TempData


I have a problem with something apparently simple, however all of the answers I find in SO and Google are not applicable.

I am creating an MVC page for an existing system, which requires:

  • the initial Get method to pass two parameters (that enables the 'context' of the page) - jobId and parentId.
  • session state is disabled, therefore TempData is not an option.

So, I have an Index() method which builds a model based on the two parameters and data from DB.

 public ActionResult Index(int jobId, int? parentId = null)
    {
        //build model based on Id params
        var model = new JobModel
        {
            ...
        };
        return View(model);
    }

The view has got several forms on it that allow some actions to be carried out on the model (add files, remove files)

         @using (Html.BeginForm("AddNewFiles", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
        {
            @Html.HiddenFor(m => m.JobId)
            @Html.HiddenFor(m => m.ParentId)
            @Html.TextBoxFor(m => m.AdditionalFiles, new {type = "file", multiple = "true"})
            @Html.ValidationMessageFor(m => m.AdditionalFiles)
            <input type="submit" name="SubmitNewFiles" id="SubmitNewFiles" value="Upload"/>
        }

Submission of this form moves me to the proper Post action

    [HttpPost]
    public ActionResult AddNewFiles(JobModel jobModel)
    {
        if (!ModelState.IsValid)
        {
            throw new InvalidDataException(message: "Model is invalid");
        }
        //doing some stuff with the files here
        //...

        //now I want to get back to the initial page with forms etc, however show a temporary label somewhere saying that 'something happened OK'
        return RedirectToAction("Index", routeValues: new RouteValueDictionary(values: new
        {
            jobId = job.Id,
            parentId = job.OrganizationId
        }));
    }

Now, the problem is that I don't know how can I show a message on the page. As I said, I cannot use TempData (which is 50% suggested answers).

Other solutions include return View("Index") instead of return RedirectToAction("Index") - however in this case I don't know how could I pass the proper parameters for my Index(int jobId, int? parentId = null) method, so that the updated model can be generated.

I am just beginning with MVC so I suppose the answer is pretty simple, however all the things I found are not applicable in my case I think. Cheers!


Solution

  • Without using TempData you have a couple of options:

    • Supply the message via QueryString, where you don't even need to change your target controller action, you can just capture the value using Request.QueryString["key"] - if you wish to keep the target action as is
    • Store a temporary cookie that you eliminate on the target action (sounds a bit overkill but it is an option)
    • If you do not want to pass the message in plain text you can use (via QS for eg) an id for a message you have on a Lookup or Dictionary or even your underlying storage system and just use that undecipherable id on the target action to retrieve the message and send it to the view

    Once you have the message or the id on the target action you can use ViewBag, ViewData or the Model itself to provide that info to the view.