0

In my form, I've tried setting the id = Model.SearchTerm but the doesn't work as when I go to the form page it says "Object reference not set to an instance of an object." which makes sense, as the user hasn't entered a search term yet.

For example, on our Facilities page we have several ways to find a facility. In this case, when you search by zipcode, you get the URL as http://localhost:50264/Facilities/SearchByZipCode.

Here's the form:

@if (@ViewBag.ZipCodeSerachError != null)
{
    @ViewBag.ZipCodeSerachError
}
@using (Html.BeginForm("SearchByZipCode", "Facilities", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.LabelFor(m => m.SearchTerm, "5 Digit Zip Code:")

    @Html.TextBoxFor(m => m.SearchTerm, new { @class = "form-control", maxlength = 5, id = "zipCodeSearch" })
    <br />
    @Html.LabelFor(m => m.Distance, "List facilities within:")

    @Html.DropDownList("Distance", ViewBag.ZipCodeDistanceList as IEnumerable<SelectListItem>, new { @class = "form-control" })
    <br />
    <button type="submit" class="btn btn-default">Search</button>
}

When you post, you get the following ActionResult:

[HttpPost]
public ActionResult SearchByZipCode(FacilitiesSearchViewModel.FacilitiesByZipCode vm)
{
    var zipCode = oandpService.GetZipCodes().FirstOrDefault(x => x.ZipCodeId == vm.SearchTerm);

    var facilities = new List<Facility>();

    if (zipCode == null)
    {
        ViewBag.ZipCodeSerachError = "An Invalid or Unknown US Zip Code was Entered!";
        facilities = new List<Facility>();
    }
    else
    {
        var zipCodeId = oandpService.GetZipCode(vm.SearchTerm);
        var zipCodes = oandpService.GetZipCodesByDistance(string.Format("{0} {1}", zipCodeId.ZipLongitude, zipCodeId.ZipLatitude), vm.Distance.GetValueOrDefault(0)).Select(x=> new ZipCodeWithDistance() { ZipCode = x.ZipCode, ZipLatitude = x.ZipLatitude, ZipLongitude = x.ZipLongitude, Distance = x.Distance}).ToList();

        facilities = oandpService.GetFacilitiesByZipCodes(zipCodes).OrderByDescending(x=>x.IsFeatured).ThenBy(x=>x.Name).ToList();
    }
    var results = new FacilitiesSearchViewModel.FacilitiesByZipCode
    {
        SearchTerm = vm.SearchTerm,
        Distance = vm.Distance,
        Facilities = facilities
    };

    return View("SearchByZipCode", results);
}

So, the question is, how can I make it so when someone searches for a Facility, it makes the URL http://localhost:50264/Facilities/SearchByZipCode?Distance=50&ZipCode=32608

I'd like to get that done mainly because I'd like to make our SiteMap to capture SearchByZipCode with the preservedRouteParameters being equal to "distance, zipcode".

Thank you for all your help. If you need more information let me know.

2 Answers 2

1

For http://localhost:50264/Facilities/SearchByZipCode?Distance=50&ZipCode=32608, you will need HttpGet.

Easiest way is to create another method overload with distance and zipCode parameters.

[HttpGet]
public ActionResult SearchByZipCode(string distance, string zipCode)
{
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

your requirement url is like HttpGet based (like querystring)

so you need to make your method as HttpGet in controller.

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.