0

I am using Razorview and Entity Framework and I am trying to achieve the following: I have a dropdown(id=ColumnName) that has a list of column names and a textbox(id=SearchValue) for value to be searched from a table. On click of a button, I am trying to retrieve from the database, the record where the column name is 'ColumnName' selected and value='SearchValue'. I am getting a 404 Resource Not found error on click of the button. I am not sure where I am going wrong. My code is as follows:

HTML:

<select id='mySelector' name="ColumnName">
    <option value="">Please Select</option>
    <option value='Country'>Country</option>
    <option value='Title'>Title</option>
    <option value='State'>State</option>
</select>
<input type="text" id="cs" name="SearchValue">
<input type="button" value="Search" onclick="location.href='@Url.Action("FilterByColumn", "CountryController")?SearchValue=' + document.getElementById('cs').value + '&ColumnName=' +document.getElementById('mySelector').value" />
<table id='myTable'>
// values
</table>

CountryController:

public ActionResult FilterByColumn(String ColumnName, String SearchValue)
{

    if (!String.IsNullOrEmpty(SearchValue))
    {
        List<Country> result = new List<Country>();
        result = db.Countries.ToList();
        result = db.Countries.Where(ColumnName + ".Contains" + "(\"" + SearchValue.ToLower() + "\")").ToList();
        return View(result);
    }
    return RedirectToAction("Index");
}

Note: I have other methods like create,edit in this controller. Also the URL rendered which throws the 404 error is : /CountryController/FilterByColumn?SearchValue=sample&ColumnName=Title

9
  • Unless your have a controller named CountryControllerController (which I doubt), its @Url.Action("FilterByColumn", "Country") Commented Aug 13, 2017 at 6:44
  • My controller is called CountryController.cs and the url rendered seems to be correct : /CountryController/FilterByColumn Commented Aug 13, 2017 at 6:52
  • No its not!! Your url needs to be /Country/FilterByColumn (read the previous comment) Commented Aug 13, 2017 at 6:55
  • I apologize if this is redundant but I am new to this. I did try what you said but I am now getting this error: The view 'FilterByColumn' or its master was not found or no view engine supports the searched locations. It seems to be searching for the 'FilterByColumn' method as a view. Commented Aug 13, 2017 at 7:02
  • That is because you do not have a view named FilterByColumn.cshtml. If you want to return a specific view (which dos not match the name of the method), then its return View("yourViewName", result); Commented Aug 13, 2017 at 7:05

2 Answers 2

1

Your code as flollows

   <input type="button" value="Search" onclick="location.href='@Url.Action("FilterByColumn", "CountryController")?SearchValue=' + document.getElementById('cs').value + '&ColumnName=' +document.getElementById('mySelector').value" />

Never give CountryController, just Give Country and try. Edit your code as follows and try

  <input type="button" value="Search" onclick="location.href='@Url.Action("FilterByColumn", "Country")?SearchValue=' + document.getElementById('cs').value + '&ColumnName=' +document.getElementById('mySelector').value" />

and give the parameters in same order in both razor and controller

Sign up to request clarification or add additional context in comments.

Comments

0

your button code as follows

input type="button" value="Search" onclick="location.href='@Url.Action("FilterByColumn", "CountryController")?SearchValue=' + document.getElementById('cs').value + '&ColumnName=' +document.getElementById('mySelector').value" />

but in the controller you have written as follows

public ActionResult FilterByColumn(String ColumnName, String SearchValue)

but the order of parameters are different. So change your controller as follows.

public ActionResult FilterByColumn(String SearchValue,String ColumnName)

Hope this will work out.

1 Comment

The order of parameters makes no difference

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.