2

I'm new to ASP.NET MVC; something I'm having trouble with is getting data from a jqueryUI dialog form and passing it to the controller so that I can, for instance, save form input to a database.

The method in my controller for handling this doesn't get called for some reason, despite the fact that it seems to be referenced correctly in the jquery code. Here is what I have in the view (I've obviously omitted most of the code, just putting the relevant parts here for readability):

<a href="#" id="dialog_link">Open Dialog Box</a>



<div id="dialog" title="MakeSomeSampleNote">
    <form id="testform" method="post">
        <input id="tbx_casenote" name="tbx_casenote" type="text" style="height:50px; width:200px;" />
    </form>
</div>


$("#dialog").dialog({
    autoOpen: false,
    resizable: true,
    height: 210,
    width: 510,
    modal: true,
    buttons: {
        "Ok": function () {
             var noteval = $("#tbx-casenote").val();
             $.ajax({
             type: "POST",
             url: "@Url.Action("NoteController/SaveNote")",
             data: { content: tbx_casenote_val },
             cache: false,
             dataType: "json",
             success: function (data) {
                     $("#dialog").dialog("close");
                     }
             });
             },
             "Cancel": function () {
                    $(this).dialog("close");
             }}
      });





$('dialog_link').click(function () { 
      $('#dialog').dialog('open');
      return false;
 });

Here is the method I'm using in the controller; when debugging, this method never gets hit after I test the dialog form and click the "Ok" button:

 [HttpPost]
 public JsonResult SaveNote(string NoteText)
 {
      //code to save this note to database - not relevant to question
      return Json(new { success = true });

 }

Thanks.

3
  • Since you have set autoOpen: false where are you opening/showing the dialog? Also you seem to be using some tbx_casenote javascript variable in your AJAX call which is not clear where is defined. Commented Jan 21, 2012 at 22:38
  • Edited my original post to answer your questions. Thanks Commented Jan 22, 2012 at 5:03
  • Ok, I made some changes to the code and have updated my post to reflect that. No longer getting the 'out of stack space' error. Now it's just that nothing seems to be happening; the SaveNote method in the controller is never called from jquery, and that's where it's at right now. Commented Jan 22, 2012 at 15:03

2 Answers 2

2

It looks to me like your @Url.Action is wrong. Have you tried @Url.Action("SaveNote", "Note")?

Just to make this a complete answer, the problem was that the word "Controller" should not appear in the Url.Action call. Each of the following would work:

@Url.Action("SaveNote", "Note")

...or

@Url.Action("Note/SaveNote")

Sign up to request clarification or add additional context in comments.

2 Comments

Changing it to Note (instead of NoteController) fixed it. Works fine now. Thanks! I was really getting frustrated with this. Somehow I knew I was just doing something dumb and it would be a simple fix.
Correct. The "Controller" suffix is a convention in ASP.NET MVC. You should only reference the Controller by the "Note" part.
1

you can make url as "controlname/methodname"

2 Comments

I did not know that. Even so, should it be "NoteController/SaveNote"? Or just "Note/SaveNote"? I doubt the controller is named NoteControllerController.
if your controller name is "LoadController" you take Load only,i use this technique with asp.net url:"pagename.aspx/methodname" but make attribute[webmethod] over my method in page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.