0

When submitting a form by clicking a submit button I am getting all the values in placeType but when I click on "<<<" @Html.RouteLink, placeType is null.

I need placeType values to perform pagination. What can I do to pass placeType from RazorView to Controller using @Html.RouteLink.

 @using (Html.BeginForm("PagedPlacesIndex", "Place", FormMethod.Get))
      {
        @Html.AntiForgeryToken()
         <div class="form-horizontal">
       @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.ThemePark, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.ThemePark)
                    @Html.ValidationMessageFor(model => model.PlaceType.ThemePark, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.Museum, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.Museum)
                    @Html.ValidationMessageFor(model => model.PlaceType.Museum, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.Zoo, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.Zoo)
                    @Html.ValidationMessageFor(model => model.PlaceType.Zoo, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.IndoorPlayarea, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.IndoorPlayarea)
                    @Html.ValidationMessageFor(model => model.PlaceType.IndoorPlayarea, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.WaterPark, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.WaterPark)
                    @Html.ValidationMessageFor(model => model.PlaceType.WaterPark, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.IndoorActivities, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.IndoorActivities)
                    @Html.ValidationMessageFor(model => model.PlaceType.IndoorActivities, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.OutdoorActivities, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.OutdoorActivities)
                    @Html.ValidationMessageFor(model => model.PlaceType.OutdoorActivities, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.NatureReserve, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.NatureReserve)
                    @Html.ValidationMessageFor(model => model.PlaceType.NatureReserve, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.ArtCraftGallaries, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.ArtCraftGallaries)
                    @Html.ValidationMessageFor(model => model.PlaceType.ArtCraftGallaries, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.HistoryHeritage, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.HistoryHeritage)
                    @Html.ValidationMessageFor(model => model.PlaceType.HistoryHeritage, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            @Html.LabelFor(model => model.PlaceType.SportLeisure, htmlAttributes: new { @class = "control-label col-md-9" })
            <div class="col-md-1">
                <div class="checkbox">
                    @Html.EditorFor(model => model.PlaceType.SportLeisure)
                    @Html.ValidationMessageFor(model => model.PlaceType.SportLeisure, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

      <div class="form-group col-lg-3 col-md-3 col-sm-3">
        @Html.LabelFor(model => model.PlaceType.Beach, htmlAttributes: new { @class = "control-label col-md-9" })
        <div class="col-md-1">
          <div class="checkbox">
            @Html.EditorFor(model => model.PlaceType.Beach)
            @Html.ValidationMessageFor(model => model.PlaceType.Beach, "", new { @class = "text-danger" })
          </div>
        </div>
      </div>
      <div class="form-group col-lg-3 col-md-3 col-sm-3">
        @Html.Label("sortOrder", htmlAttributes: new { @class = "control-label col-md-9" })
        <div class="col-md-1">
          <div class="checkbox">
            @Html.DropDownList("sortOrder", new List<SelectListItem>
            {
              new SelectListItem{ Text="Name Descending", Value = "name_desc" },
              new SelectListItem{ Text="Name Ascending", Value = "" },
              new SelectListItem{ Text="Date Descending", Value = "date_desc" },
              new SelectListItem{ Text="Date Ascending", Value = "Date" }
            },null,new {@class= "dropdown - menu" })
            @Html.ValidationMessageFor(model => model.PlaceType.Beach, "", new { @class = "text-danger" })
          </div>
        </div>
      </div>

      <input type="hidden" name="selectionChecked" value="true" />
        <div class="form-group col-lg-3 col-md-3 col-sm-3">
            <div class="col-md-offset-8  col-md-3">
                <input type="submit" value="Update Search" class="btn btn-xs btn-success" />
            </div>
        </div>
    </div>
@if(Model.HasPreviousPage)
{
@Html.RouteLink("<<<", "Places", new
{
  page = (Model.PageIndex - 1), placeType = Model.PlaceType
})
}
@if(Model.HasNextPage)
{
@Html.RouteLink(">>>", "Places", new {page = (Model.PageIndex + 1), placeType = Model.PlaceType})
}

in Place Controller :-

public ActionResult PagedPlacesIndex(string sortOrder, int? page, bool? selectionChecked, PlaceType placeType)
    {

      IEnumerable<Place> places = _db.Places;
      IEnumerable<Place> sortedPlaces = SortPlaces(places, sortOrder);
      IEnumerable<Place> filteredPlaces = null;
      if (placeType != null)
      {
        bool placeTypeCheck = placeSetorNotCheck(placeType);
        if (placeTypeCheck)
        {
          filteredPlaces = FilteredPlaces(sortedPlaces, placeType);
        }
        else
        {
          filteredPlaces = sortedPlaces;

        }
      }
      else { filteredPlaces = sortedPlaces; }

      page = page ?? 0;
      const int pageSize = 5;
      var pagedPlaces = new PaginatedList<Place>(filteredPlaces,placeType, page ?? 0, pageSize);
      return View(pagedPlaces);
    }
santa
  • 1
  • 1
    99% of this code is irrelevant. You cannot pass a complex object to a GET method like that (look at the url it is generating) - you can add query string values for each property of `PlaceType` –  Sep 17 '18 at 23:18
  • Thanks for your help. Is query string the only way ( as query string get really lengthy) . If u can suggest any other way around to implement pagination while using "placeType selections " ? I would really appreciate your help thanks. – santa Sep 18 '18 at 12:49
  • Yes a query string is the only way when its a GET (and you are generating a query string with you current code. But you can always add a `Page` property to your `PlaceType` model, and then you can use `@Html.RouteLink(">>>", "Places", Model.PlaceType )` - also refer [Can we pass model as a parameter in RedirectToAction?](https://stackoverflow.com/questions/22505674/can-we-pass-model-as-a-parameter-in-redirecttoaction/37198363#37198363) - the same applies to `RouteLink()` or `ActionLink()` etc –  Sep 18 '18 at 12:53

0 Answers0