I am passing a List<Listing> object to another action method and making that method call a View with the parameter.
For some reason, the parameter which I am passing is null.
The following works fine:
public ActionResult SortListing(string categoryGuid)
{
var listingCategory = new ListingCategory();
listingCategory = _tourismAdminService.GetByGuid<ListingCategory>(Guid.Parse(categoryGuid));
var listings = new List<Listing>();
foreach (var listing in _tourismAdminService.ListAllEntities<Listing>())
{
if (listing.CategoryId == listingCategory.Id)
{
listings.Add(listing);
}
}
return RedirectToAction("Index", "Listing", listings);
}
The following shows a Parameter which is coming up as null.
public ActionResult Index(List<Listing> listing)
{
var model = new ListListingsViewModel();
IEnumerable<ListingCategory> categories = _service.ListAllEntities<ListingCategory>();
if (categories != null)
{
model.Categories =
categories.Select(
cat =>
new SelectListItem
{
Text =
cat.GetTranslation(stringA,
stringB).Name,
Value = cat.Guid.ToString()
}).ToList();
}
model.Listings = listing ?? _service.ListAllEntities<Listing>();
return View(model);
}
EDIT
Error Message:
The ViewData item that has the key ‘SelectedCategoryGuid’ is of type ‘System.Guid’ but must be of type ‘IEnumerable’.
On:
@Html.DropDownListFor(
m => m.SelectedCategoryGuid,
Model.Categories,
"Select a Category",
new {
id = "hhh",
data_url = Url.Action("SortListing", "Listing")
}
)
RedirectToActionmethod returns a Http 302 response to the browser,which cause the browser to make a GET request to the specified action.Remember HTTP is stateless. You can not pass such complex object like that.
You should either pass a querystring (an Id) and get the value in the second action again or keep the data in a persitant medium between the calls. You may consider using Session or
TempData(session is the backed up storage for that) for that.http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
EDIT : As per the comment. Yes you can call the view from the first method itself. the below code will pass the string collection the
Indexview (index.cshtml).If you want to pass the data to a view in a different controller, you can specify the full path when calling the View method.
Assuming your view is strongly typed to a list of
stringslike this