MVC - C# -Passing List of string object using ViewBag and ViewData to View from Controller

  • Create and pass list of string from Controller to View

      HomeController.cs

namespace MvcVersionFinder.Controllers
{
    public class HomeController : Controller
    {
          public ActionResult VBDemoPassingListofStr() // ViewBagDemo
        {
            List lstofCountries = new List { "India", "Germany", "England" };
            ViewBag.countries = lstofCountries;
            return View();
        }

     }
}


    
   VBDemoPassingListofStr.cshtml


@{
    ViewBag.Title = "VBDemoPassingListofStr";
}

    @foreach (string country in @ViewBag.countries)
    {
       

  •         @country
       

  • }




    No comments:

    Post a Comment