1

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 });
        }
    }
3
  • 1
    In your view , do you use ajax? Commented Jan 18, 2023 at 7:26
  • 1
    You can create a model to contain your values and pass the model from controller to the view. Commented Jan 18, 2023 at 7:36
  • 1
    No ajax used in view. Commented Jan 18, 2023 at 11:59

2 Answers 2

3

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`
Sign up to request clarification or add additional context in comments.

1 Comment

thanks buddy. It may be helpful but I have some return statements and they are needed to be redirected to previous page. Any idea about how send these data to view without View() method?
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.