Domanda

I want to create a template to format some ViewBags.
Up to now, I had only the case of property of of a class, which I could easily modify with

[UIHint("MYTemplate")]
public virtual float Foo { get; set; } 

Every selction of the property will consider "MyTemplate" and formate the output correctly.

How am I able to transfer this 'structure' to a ViewBag, which is defined in a certain controller?

È stato utile?

Soluzione

Actuall, you can't. The contents of the ViewBag is Dynamically Resolved at Runtime. As opposed to Model properties. You can only use Templates for properties of a Model.

The only other option would be to add it to your model. The reason behind this is that, Your model (or View Model to be precise) ought to have everything that your view needs to display data. The ViewBag was intended to add some flexibility for data that doesn't necessarily need to be in your model like the contents of a DropDownList. Therefore, if you need to display content from the ViewBag like properties in your page, then you should consider promoting that property to your model.

Alternatively, you could use Partial Views. like so @Html.Partial("_MyTemplate",ViewBag.Data)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top