MVC / HTML - input view will not be called when HTML is in the text area

Any idea why the below code won't run if I have to put some HTML inside a textbox? It works great, I don't have HTML, 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 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 source to share


4 answers


Ended up being a test. Added the following to Web.config: It works.



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

      

+2


a source


In your controller action, add the following attribute:

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

      



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

+4


a source


Why didn't you use Ajax.BeginForm

? You need to configure the function parameters in the method of post post, for example public ActionResult AddPost(string Title, string Description)

. Note that the parameter names must match the IDs of your controls where the data is coming from.

+1


a source


Nested forms are not supported in the HTML standard and that might be the cause of your problem. Try removing one of the nested forms and see if that helps.

0


a source







All Articles