将对象数组作为TempData []传递以查看 [英] Passing an object array as TempData[] to view

查看:134
本文介绍了将对象数组作为TempData []传递以查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将后动作中的两个值返回到RedirectToAction中的视图. TempData[]似乎是理想的选择,因为数据仅在用户保存后用于显示成功消息.

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.

当前,我将所有数据作为new MvcHtmlString

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//> " ) ;

我想以object[]

TempData["SaveMsg"] = new object[]{model.ImageUrl , model.Name}

然后我将能够将对象传递到HtmlHelper并编写消息显示的条件.

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'

这有可能吗?

推荐答案

您可以在视图中访问它们,方法是先将它们转换为对象数组,然后对其进行索引,即

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小提琴

这篇关于将对象数组作为TempData []传递以查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆