Using Ajax, the text field is not cleared when submitted to MVC

I need your help. I am using AjaxBeginform to post a question in a small text box. I can ask a question and it works great for db, however the question is never cleared. I use Ajax because I do not need the whole page to publish. I tried using this.rest (); but it works in IE, but it will not work in Firefox and Chrome. I tried $ ('# question'). Reset (); But that still doesn't work. I am sure that this is what I am doing wrong.

Here is my code below. Thanks for the help.

These are my scripts at the top of the page:

<script type="text/javascript" src="~/Scripts/jquery-migrate- 1.2.1.min.js"></script> 

Here is an AjaxBeginForm along with a text box

  @using (Ajax.BeginForm("SendQuestion", new { PresId = Model.PresentationID, catalogId = ViewBag.CatalogId }, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "questionTxt", OnBegin = "OnBegin", OnSuccess = "OnSuccess", OnFailure = "OnFailure" })) { <fieldset id="question"> <p> <textarea id="questionTxt" style="font-size:12px" cols="20" rows="6" name="questionTxt" onclick="javascript:removeTextAreaWhiteSpace(this);"></textarea> </p> <button id="question-btn" type="submit">Submit</button> </fieldset> } 

Here is my function for OnSuccess

  function OnSuccess() { alert("Your question has been submitted."); $('#question').val(''); } 

Here is my controller

  public bool SendQuestion(string questionTxt, long PresId, long catalogId) { try { var db = new ODTEntities(); var pres = db.Presentations.FirstOrDefault(i => i.PresentationID == PresId); var subject = db.Catalogs.FirstOrDefault(i => i.CatalogID == catalogId).CatalogCode + " - " + pres.Title + " - " + pres.Presenter.PresenterName; Utils.AddQuestionToDB(db, PresId, catalogId, Utils.GetAttendeeId(), questionTxt); string body = "This question : " + Environment.NewLine + questionTxt + Environment.NewLine + " was sent by " + User.Identity.Name; var catalog = db.Catalogs.FirstOrDefault(i => i.CatalogID == catalogId); if (!string.IsNullOrEmpty(catalog.QuestionsEmail)) Helper.SendMail(body, subject, catalog.QuestionsEmail, ""); else Helper.SendMail(body, subject); } catch (Exception) { return false; } return true; } 

Now, as I said, I tried this one and it will clear in IE, but not in Firefox or Chrome. Therefore, when I submit the form, the pop-up message returns with the answer to your question, and the pop-up window is visible, the question changes to true, and then you click ok in the pop-up window, the text field is cleared in IE:

  function OnSuccess() { alert("Your question has been submitted."); $('#question').val(''); this.reset(); } 

If someone can tell me what I'm doing wrong, that would be greatly appreciated. Thanks.

+6
source share
2 answers
 function OnSuccess() { alert("Your question has been submitted."); this.reset(); $('#questionTxt').val(''); //Modified, put it AFTER reset() } 

questionTxt is not a question

+2
source

Give your form an identifier, and then try resetting it:

 @using (Ajax.BeginForm("SendQuestion", new { PresId = Model.PresentationID, catalogId = ViewBag.CatalogId }, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "questionTxt", OnBegin = "OnBegin", OnSuccess = "OnSuccess", OnFailure = "OnFailure" },new {id="formid"})) } $('#formid').reset(); // BY JQUERY document.getElementById("formid").reset(); // BY PURE JAVASCRIPT 

Or, if you have only one form per page, then this will also work, since it only works for the first form:

 $('form:first').reset(); 
+2
source

All Articles