views:

40

answers:

2

Hi, I have a simple for with at text field where the user enters the address of a RSS feed. I wont act if the field is blank, this is my markup:

<%=Html.ValidationSummary() %>
    <table>
        <tr>
            <td>
                Feed Url:
            </td>
            <td>
                <%=Html.TextBox("url", null, new {@style="width:300px"}) %>
            </td>
        </tr></table>

My controller is also very simple:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddFeed(FormCollection collection)
    {
        string url = collection.Get("url");
        string roles = collection.Get("Roles");
        if (string.IsNullOrEmpty(url))
        {
            ModelState.AddModelError("url", "Please provide a propre feed url");
        }
        if (string.IsNullOrEmpty(roles))
        {
            ModelState.AddModelError("Roles", "Please select a valid role");
        }
        if (ModelState.IsValid)
        {
            Session["url"] = url;
            Session["Roles"] = roles;
            return RedirectToAction("ValidateFeed");
        }
        else
        {
            return View();
        }
    }

When it fails it reloads the view and makes an exception in the line where it renders my textbox, saying that there has been a null pointer exception. This really botheres me, it should be so simple... but still im struggling

/H4mm3r

Edit Please disregard the Roles element its a drop down i have, but removed it from markup for simplicity

A: 

I think you cant use the ModelSate like that if you didn't do something like:

UpdateModel(collection);

or

TryUpdateModel(collection);

or by Parameter Binding

public ActionResult AddFeed(YourModelType collection)

Because the Modelstate is not associated with any Model.

As the MSDN reference says:

ModelState Class

Encapsulates the state of model binding to a property of an action-method argument, or to the argument itself.

Omar
A: 

You probably can't use null when using the HTML helper. Try this in your form instead:

<%= Html.TextBox("url", String.Empty, new {@style="width:300px"}) %>

I haven't tested that, but that's the first thing I would try, given that error message.

Jay Querido
Tried it :-) no luck :-( Think "Omar" has a point in his answer, trying it our now
H4mm3rHead