问题
I would like to return two values from a post action to the view in a RedirectToAction
. TempData[]
seems like the ideal option , as the data is only used to show a success message once the user has saved.
I would like to show a small thumbnail of the image that the user just saved and the title of the saved item, in the success message.
Currently I am passing all the data as a new MvcHtmlString
TempData["SaveMsg"] = new MvcHtmlString("<img src=" + model.ImageUrl + " //> <h3//>" + model.Name + " has been saved.<//h3//> " ) ;
I would like to send it as an object[]
TempData["SaveMsg"] = new object[]{model.ImageUrl , model.Name}
Then I would be able to pass the objects into an HtmlHelper
and write the conditions for the message display.
I just do not know how to access the object in the view
@TempData["SaveMsg"][0] // (O.o) // Error Cannot apply indexing with
// [] to an expression of type 'object'
Is this even possible?
回答1:
You access them in a view by casting them to an object array first then indexing them i.e.
@{
var objectArray = (object[]) TempData["SaveMsg"];
}
@objectArray[0]
@objectArray[1]
.Net fiddle
回答2:
@TempData["SaveMsg"][0] will not work.
Try something like this
obj[] saveMsgs = (obj[])TempData["SaveMsg"];
来源:https://stackoverflow.com/questions/29510101/passing-an-object-array-as-tempdata-to-view