views:

153

answers:

4

Any idea why the below code doesn't trigger if I were to put some HTML inside the textarea? It works fine it I don't have HTML in it, but I'm not sure why it doesn't work with it. Here is the code.

    <% using (Ajax.BeginForm("AddPost", new AjaxOptions { UpdateTargetId = "blogPosts" }))
   { %>
    <table>
        <tr>
            <td>Post Title:</td>
            <td><input id="Title" type="text" name="title" /></td>
        </tr>
        <tr>
            <td>Post Description:</td>
            <td><textarea id="Description" name="description" rows="10" cols="60" wrap="virtual"></textarea></td>
        </tr>
    </table>
    <input type="submit" value="Save" />
<%} %>


    Here is what gets rendered (It's inside an Ajax form)

        <div>
        <form action="/Home/AddPost" method="post" onclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));" onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: &#39;blogPosts&#39; });">
    <table>
        <tr>
            <td>Post Title:</td>
            <td><input id="Title" type="text" name="title" /></td>
        </tr>
        <tr>
            <td>Post Description:</td>
            <td><textarea id="Description" name="description" rows="10" cols="60" wrap="virtual"></textarea></td>
        </tr>
    </table>
    <input type="submit" value="Save" />
</form>
    </div>
+2  A: 

In your controller action add the following attribute:

[ValidateInput(false)]
public ActionResult AddPost() { }

By default MVC will check for HTML input in the form and throw an exception unless you tell it not to validate the request by placing the ValidateInputAttribute on your controller action.

Nathan Taylor
Negative. It's not even getting to the code when I hit the button. I'm using VS2010 MVC2.0 if that helps.
cw
A: 

Nested forms aren't supported in the HTML standard and that might be what is causing your issue. Try removing one of the nested forms and see if that helps.

amurra
Removed it. Doesn't work.
cw
+1  A: 

Why couldn't you have used Ajax.BeginForm? You need to set up function parameters in your controller post method like so: public ActionResult AddPost(string Title, string Description). Note that the parameter names have to match the IDs of your controls from where the data is coming from.

Jay Shanker
I already have it setup like this. As previously said, it doesn't work only when you enter a < with a letter after it like <a
cw
+2  A: 

Ended up being a validation thing. Added the following to the Web.config and it worked.

<httpRuntime requestValidationMode="2.0" />
<pages validateRequest="false" />
cw