tags:

views:

99

answers:

3

Inside of an asp.net mvc partial view, I have an Ajax form that posts a value and replaces the contents of its parent container with another instance of the form.

Index.aspx view:

<div id="tags">
    <% Html.RenderPartial("Tags", Model); %>
</div>

Tags.ascx partial view:

<% using(Ajax.BeginForm("tag", new AjaxOptions { UpdateTargetId = "tags" }))
    { %>
        Add tag: <%= Html.TextBox("tagName")%>
        <input type="submit" value="Add" />
<%  } %>

The controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tag(string tagName) {
    // do stuff
    return PartialView("Tags", ...);
}

The problem is when the new instance of the form returns, the posted value is already stored in the input field. As in, whatever I posted as the 'tagName' will stay in the textbox. Firebug shows that the value is hardcoded in the response.

Is there any way to clear the input textbox's value when returning the partial view?

I've tried:

<%= Html.TextBox("tagName", string.Empty)%>

and

<%= Html.TextBox("tagName", string.Empty, new { value = "" })%>`

neither of which do anything.

EDIT:

I realize there are js solutions, which I may end up having to use, but I was wondering if there were any ways of doing it in the backend?

+1  A: 

I'm not sure if this solution is "good enough" for you, but couldn't you just empty the form in a JS callback function from your ajax call? If you're using jQuery on your site, the callback function could look something like this:

function emptyFormOnReturn() {
    $(':input').val();
}

I am not entirely sure if it will, but in case the above code also removes the text on your submit button, change the selector to ':input[type!=submit]'.

Tomas Lycken
A: 

Well, this may seem like a dirty solution, but you could always set the OnCompleted property of AjaxActions and then run some jQuery like $('#tagName').val('').

Jon Freeland
A: 

yes you should use jquery to set values on response if you change your code to use jquery for ajax operations, you can call you settingvalues function on success callback...example:

http://docs.jquery.com/Ajax/jQuery.ajax#options

Marko