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: '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" />
</form>
</div>
a source to share
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.
a source to share
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.
a source to share