asp.net MVC ActionMethod causing null exception on parameter
- by David Liddle
I am trying to add filter functionality that sends a GET request to the same page to filter records in my table.
The problem I am having is that the textbox complains about null object reference when a parameter is not passed. For example, when the person first views the page the url is '/mycontroller/myaction/'. Then when they apply a filter and submit the form it should be something like 'mycontroller/myaction?name=...'
Obviously the problem stems from when the name value has not been passed (null) it is still trying to be bound to the 'name' textbox. Any suggestions on what I should do regarding this problem?
UPDATE
I have tried setting the DefaultValue attribute but I am assuming this is only for route values and not query string values ActionResult MyAction([DefaultValue("")] string name)
//Action - /mycontroler/myaction
ActionResult MyAction(string name)
{
...do stuff
}
//View
<div id="filter">
<% Html.BeginForm("myaction", "mycontroller", FormMethod.Get); %>
Name: <%= Html.TextBox("name") %>
....
</div>
<table>...my list of filtered data