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:
jobId
and parentId
.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!
Without using TempData you have a couple of options:
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.