Home > Software design >  How to pass list of strings to view with action result method in ASP dotNet Core
How to pass list of strings to view with action result method in ASP dotNet Core

Time:01-18

I have an ActionResult Method which i wanted to return some values to the view for using in a form to submit afterwards. How can I access these data from view for submition in a form?!

Here is my ActionResult method:

  

    [HttpPost]
    public virtual async Task<IActionResult> ImportPhonenumbersFromExcel(IFormFile importexcelfile, int currentFestivalId)
    {

        if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageFestivals))
            return AccessDeniedView();

        try
        {
            if (importexcelfile != null && importexcelfile.Length > 0)
            {
                var result = await _importManager.ImportPhonenumbersFromXlsxAsync(importexcelfile.OpenReadStream());

            }
            else
            {
                _notificationService.ErrorNotification(await _localizationService.GetResourceAsync("Admin.Common.UploadFile"));
                return RedirectToAction("Edit", new { id = currentFestivalId });
            }
            _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Festival.Phonenumbers.Imported"));
            return RedirectToAction("Edit", new { id = currentFestivalId });

        }
        catch (Exception em)
        {
            await _notificationService.ErrorNotificationAsync(em);
            return RedirectToAction("Edit", new { id = currentFestivalId });
        }
    }

CodePudding user response:

You can create a view model that will contain the list and everything else you want to pass to the view.

Then in the view you can access it by using @model.

CodePudding user response:

For strongly typed data a view model is the best option. It is a class with properties that can be used to store your specific values which you want to pass to the view. To use a viewmodel:

  • Create the viewmodel class and add the properties you need.

  • Instantiate a new viewmodel object in your controller. The example shown below is from the Microsoft documentation where the viewmodel class is named Address.

     public IActionResult Contact()
     {
         ViewData["Message"] = "Your contact page.";
    
         var viewModel = new Address()
         {
             Name = "Microsoft",
             Street = "One Microsoft Way",
             City = "Redmond",
             State = "WA",
             PostalCode = "98052-6399"
         };
    
         return View(viewModel);
     }
    
  • Once you have set the values of the properties of the viewmodel object in the controller you can then add the viewmodel to the view.

  • To send the viewmodel to the view, pass it as a parameter:

    return View(viewModel);

  • Finally, add to the top of your view file:

    @model yourViewModelsAddress

To refer to the properties of your viewmodel in your view, follow this example:

`@Model.Property`
  • Related