views:

37

answers:

1

Hello Friends,

I am using this ccode right now..

 <%using (Html.BeginForm("SaveNewServiceTypeCategory","ServiceTypeCategory")){ %>
      <table>
        <tr>
          <td>Service type category:</td>
          <td style="padding-left:5px;">
            <input type="text" id="txtNewServiceTypeCategory" name="txtNewServiceTypeCategory" style="width:400px;" maxlength="30" />
          </td>
        </tr>
        <tr>
          <td valign="top">Service type description:</td>
          <td style="padding-left:5px;">
            <textarea id="txtNewServiceTypeCategoryDesc" name="txtNewServiceTypeCategoryDesc" style="width:400px;" rows="3"></textarea>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" value="Save"/>
          </td>
        </tr>
      </table>
      <%} %>

But I need to display the Popup message when data added to the Database..

I wrote somethign like this

 $(function () {
          $('#form1').submit(function () {
              $.ajax({
                  url: ,  how to give the URl here?
                  type: this.method,
                  data: $(this).serialize(),
                  success: function () {
                      alert("Saved Successfully!");
                  }
              });
              return false;
          });

After saving need to display saved Successfully message to the User?

thanks });

+2  A: 

You could start by giving your form an unique id:

<% using (Html.BeginForm(
    "SaveNewServiceTypeCategory", 
    "ServiceTypeCategory", 
    FormMethod.Post, 
    new { id = "form1" })) { %>

And then use this id to register for the submit event:

$(function () {
    $('#form1').submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                alert('Saved Successfully!');
            }
        });
        return false;
    });
});

There's also the jquery form plugin which allows you to AJAXify an existing form very easily:

$(function () {
    $('#form1').ajaxForm(function() {
        alert('Saved Successfully!');
    });
});
Darin Dimitrov
Thanks for your Update. its doing fine but its not saving to my Database at all but its giving message saved Successfully?
kumar
Well maybe the problem is in your controller action. Have you tried stepping through the code to verify what's happening?
Darin Dimitrov
Yes I am not getting the values which I am saving in to the Controller?
kumar
public ActionResult SaveNewServiceTypeCategory(string txtNewServiceTypeCategory, string txtNewServiceTypeCategoryDesc) I am getting Empty these two values..
kumar
Hmm that's strange. Normally `$(this).serialize()` should send the values from the form. Have you tried looking with FireBug what exactly is sent in the AJAX POST request to the server?
Darin Dimitrov
Hm I dint looked in to the Firebug..its strange for me..doing like this.. :)
kumar
Darin one question..if I do something like this when I click Save Button its not doing anything that is Something I need to show to the User that somethign is going on Back end?
kumar