c# - Can I use ViewData using 'for-loop' for int array? - TagMerge
3Can I use ViewData using 'for-loop' for int array?Can I use ViewData using 'for-loop' for int array?

Can I use ViewData using 'for-loop' for int array?

Asked 1 years ago
0
3 answers

Yes, you can.

You can save any object there, including arrays.

However when you access the array, you have to castto ìnt[], like this:

int a =  ((ìnt[])ViewData["Numbers"])[i];

Source: link

0

Example: ViewData in Action method Copy
public ActionResult Index()
{
    IList<Student> studentList = new List<Student>();
    studentList.Add(new Student(){ StudentName = "Bill" });
    studentList.Add(new Student(){ StudentName = "Steve" });
    studentList.Add(new Student(){ StudentName = "Ram" });

    ViewData["students"] = studentList;
  
    return View();
}
Example: Access ViewData in a Razor View Copy
<ul>
@foreach (var std in ViewData["students"] as IList<Student>)
{
    <li>
        @std.StudentName
    </li>
}
</ul>
Example: Add KeyValuePair in ViewData Copy
public ActionResult Index()
{
    ViewData.Add("Id", 1);
    ViewData.Add(new KeyValuePair<string, object>("Name", "Bill"));
    ViewData.Add(new KeyValuePair<string, object>("Age", 20));

    return View();
}
Example: ViewBag and ViewData Copy
public ActionResult Index()
{
    ViewBag.Id = 1;

    ViewData.Add("Id", 1); // throw runtime exception as it already has "Id" key
    ViewData.Add(new KeyValuePair<string, object>("Name", "Bill"));
    ViewData.Add(new KeyValuePair<string, object>("Age", 20));

    return View();
}

Source: link

0

ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows:
public class IndexModel : PageModel
{

    public void OnGet()
    {
        ViewData["MyNumber"] = 42;
        ViewData["MyString"] = "Hello World";
        ViewData["MyComplexObject"] = new Book { 
                                    Title = "Sum Of All Fears",  
                                    Author = new Author { Name = "Tom Clancy" },
                                    Price = 5.99m
                                    };
    }
}
The ViewData dictionary is automatically made available to the content page. Therefore, in order to reference values stored in it, you just refer to their item by key:
@page
@model IndexModel
@{
}
<h2>@ViewData["MyString"]</h2>

The answer to everything is @ViewData["MyNumber"]

When working with non-string values, you need to cast them to their correct type in the content page:
@page
@model IndexModel
@{
    var book = (Book)ViewData["MyComplexObject"];
}

<h2>@book.Title</h2>

@book.Author.Name

@book.Price

The ViewData attribute was introduced in ASP.NET Core 2.1. PageModel properties decorated with this attribute are automatically added as keys to the ViewData dictionary along with any value that has been assigned to them. In the following example, the Message property has been automatically added to ViewData:
public class IndexModel : PageModel
{
    [ViewData]
    public string Message { get; set; }

    public void OnGet()
    {
        Message = "Hello World";
    }
}
Now the Message property can be accessed in the view via the Model property or the ViewData dictionary:
@page
@model IndexModel
@{
    
}

<h2>@Model.Message</h2>
<h2>@ViewData["Message"]</h>

Source: link

Recent Questions on c#

    Programming Languages