2

I'm aware that this is basic but then again I couldn't figure out answer for this one ...

I would like to send string and int value as one ViewData value to View

ViewData["TypeOfPaymentSelected"] = new { StringName = Enum.GetName(typeof(PaymentType), payment), IntValue = payment };

but I do not know how to cast it inside View.

If I'm still in controller I would simple access is as

varName.StringName 
varName.IntValue

I know how to make ViewModel classes and cast it in View, I know I can send this as two ViewData values etc but I thought this can be done simpler and obvioulsy I lack basic C# knowledge

iivkovic
  • 23
  • 3

1 Answers1

0

You can use anonymous types if you want but then you need to use for example Eval-method to access it in the view:

@ViewData.Eval("TypeOfPaymentSelected.StringName")

More info in this question/answer

Preferred way would be to use a model so that everything will be strongly typed.

Example:

namespace Models
{
    public class MyModel
    {
        public string StringName;
        public int IntValue;
    }
}

Controller:

public ActionResult Index()
{
    ViewData["TypeOfPaymentSelected"] = new MyModel { StringName = "test", IntValue = 5 };
    return View();
}

And in View:

@(((Models.MyModel)ViewData["TypeOfPaymentSelected"]).StringName)
Esko
  • 4,109
  • 2
  • 22
  • 37