0

I am trying to pass a IEnumerable model parameter from one method to another however when I debug the code the parameter is not being passed even though the method signature is the same.

I've tried with and without the Bind Prefix

Method 1:

[HttpPost]
    public ActionResult Create_Filter([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
    {


        return RedirectToAction("Course", "Filter", courseFilterVM);
    }

Method 2:

public ActionResult Course([Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
    {

        return View(courseFilterVM);
    }

I am expecting the entire model to be passed but instead I am getting a null value.

smuldr
  • 315
  • 1
  • 12
  • 1
    Possible duplicate of [Passing data between different controller action methods](https://stackoverflow.com/questions/15385442/passing-data-between-different-controller-action-methods) – Mark Tallentire Feb 01 '19 at 21:06
  • I've been able to pass data before but something this specific type is tripping me up – smuldr Feb 01 '19 at 21:17
  • What you're doing here is basically sending the complex object back to the browser due to the redirect. So you're telling the client "Hey look here for the data you need instead" but the client can't then pass the IEnumerable back to the server in this way, If you want to do this you need to store the data somewhere on the server and then pick it up again in the next method. Alternatively, looking at it you're only filtering data, so pass it to a none controller class to be filtered and return the results, that way you dont need to redirect just return a view back with the new filtered data. – Mark Tallentire Feb 01 '19 at 22:02

2 Answers2

1

If you want to pass a model to the View

public ActionResult Create_Filter()
{
    return View(); 
    OR
    return View(CourseFilterVM); // Pass as a parameter
}

VIEW - Create_Filter.cshtml

@model IEnumerable<CourseFilterViewModel>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Batgirl
  • 106
  • 7
1

You can simply do as follows:

[HttpPost]
public ActionResult Create_Filter([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<CourseFilterViewModel> courseFilterVM)
{
    // Others stuffs  here

    return Course(courseFilterVM);
}
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114