asp.net MVC ActionMethod causing null exception on parameter

Posted by David Liddle on Stack Overflow See other posts from Stack Overflow or by David Liddle
Published on 2010-03-09T10:51:22Z Indexed on 2010/03/09 11:36 UTC
Read the original article Hit count: 223

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about get