开发者

MVC/HTML - input submit won't trigger when HTML is in a text area

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 c开发者_开发知识库ode.

    <% 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>


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.


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

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜