Passing an object array as TempData[] to view

試著忘記壹切 提交于 2019-12-10 18:23:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!