views:

36

answers:

2

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
A: 
//Action - /mycontroler/myaction
ActionResult MyAction(string name)
{
    if (name == null) {
        name = string.Empty;
    }
    ...do stuff
}

Or... add an overload...

//Action - /mycontroler/myaction
ActionResult MyAction()
{
     return MyAction(string.Empty);
}
Sohnee
I don't think this will work as you cannot define ActionResults's with the same name as then it will not know which route to serve.
David Liddle
@David Liddle - you can totally do this - as long as they accept different parameters. You can't define two with the same signature, but it's very common to have two methods, one which accepts the model and one which accepts no parameters.
Sohnee
The only way to do this is to supply a different ActionName as an attribute. But then you have to use different links anyway - http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc
David Liddle
A: 

Decided to implement this differently so that the input box posted to a different action method which did some business logic work and then redirected back to the original page.

POST-REDIRECT-GET

David Liddle