views:

78

answers:

3

I am pretty new at this stuff, so bear with me.

I am using ASP.NET MVC.

I have created an overlay to cover the page when someone clicks a button corresponding to a certain database entry. Because of this, ALL of my code for this functionality is in a .js file contained within my project.

What I need to do is pull the info corresponding to my entry from the database itself using an AJAX call, and place that into my textboxes. Then, after the end-user has made the desired changes, I need to update that entry's values to match the input. I've been surfing the web for a while, and have failed to find an example that fits my needs effectively.

Here is my code in my javascript file thus far:

function editOverlay(picId) {
      //pull up an overlay
      $('body').append('<div class="overlay" />');
      var $overlayClass = $('.overlay');

      $overlayClass.append('<div class="dataModal" />');
      var $data = $('.dataModal');

      overlaySetup($overlayClass, $data);


      //set up form
      $data.append('<h1>Edit Picture</h1><br /><br />');

      $data.append('Picture name: &nbsp;');
      $data.append('<input class="picName" /> <br /><br /><br />');

      $data.append('Relative url: &nbsp;');
      $data.append('<input class="picRelURL" /> <br /><br /><br />');

      $data.append('Description: &nbsp;');
      $data.append('<textarea class="picDescription" /> <br /><br /><br />');

      var $nameBox = $('.picName');
      var $urlBox = $('.picRelURL');
      var $descBox = $('.picDescription');

      var pic = null; 

//this is where I need to pull the actual object from the db
      //var imgList = 
      for (var temp in imgList)  {
          if (temp.Id == picId) {
              pic= temp;
              }
          }

  /*
      $nameBox.attr('value', pic.Name);
      $urlBox.attr('value', pic.RelativeURL);
      $descBox.attr('value', pic.Description);
  */

      //close buttons
      $data.append('<input type="button" value="Save Changes" class="saveButton" />');
      $data.append('<input type="button" value="Cancel" class="cancelButton" />');


      $('.saveButton').click(function() {
          /*
          pic.Name = $nameBox.attr('value');  
          pic.RelativeURL = $urlBox.attr('value');
          pic.Description = $descBox.attr('value');
          */

          //make a call to my Save() method in my repository

          CloseOverlay();
      });

      $('.cancelButton').click(function() {
          CloseOverlay();
      });
  }

The stuff I have commented out is what I need to accomplish and/or is not available until prior issues are resolved.

Any and all advice is appreciated! Remember, I am VERY new to this stuff (two weeks, to be exact) and will probably need highly explicit instructions.

BTW: overlaySetup() and CloseOverlay() are functions I have living someplace else.

Thanks!

+2  A: 

You cannot (and shouldn't, ever) connect to the database directly from Javascript. Even if you theoretically could (I suppose nothing's impossible) you shouldn't; you'd have to open the DB up to the public in a way that would make anyone dedicated to security pull your hair out once they'd finished with their own.

What you should do instead is find some intermediary that can act as a proxy for the database. Almost in the same way that ASP.NET does, if that's a good enough hint.

If it's not:

Create a custom ASP.NET control and have it fill your form data server-side. Have its post-back handle validation and then updating the database server-side.

Randolpho
I have to do this as an educational exercise. It MUST be done via javascript. Also, I do have a repo/controller available. I'm just not sure how to use it. Like I said, I am VERY new to this stuff.But thank you though!
Charmander
@tmedge: if you have to do it from javascript, your best bet is to skip ASP.NET MVC for this part and use a REST-ful web service that your javascript calls to retrieve and update its data.
Randolpho
@Randolpho: so you're saying that i'm doing this the worst way possible? no wonder i can't find an example on the net...sorry dude, but I really need to do this in my MVC app.
Charmander
@tmedge: You don't have to abandon ASP.NET MVC for the rest of your site, just for this part. You can add a WCF REST-ful service (using webHttpBinding) right into your ASP.NET website and consume that client-side from javascript.
Randolpho
@Randolpho: that sounds really complicated. Can you hook me up with an example please?
Charmander
@tmedge: and if your professor or whomever is driving this educational exercise tells you otherwise, they're telling you *the wrong thing*. You cannot talk to the database directly from javascript. If they suggest hackery such as hidden input forms that contain the URLs you need and that are updated immediately before a form post, they should be shot.
Randolpho
@tmedge: here's a nice series of articles on the subject: http://www.robbagby.com/rest/rest-in-wcf-blog-series-index/
Randolpho
@Randolpho: Haha that's funny. I would tell him that, but after seeing your comments I'm not sure if I'm understanding his instructions right. Thanks a lot, bro!
Charmander
Why is everyone telling him to use a WCF data service? You can do this with JQuery ajax post calling to an MVC action. Just return a JsonResult in the action. WCF just adds an unnecessary layer of complexity!
Ryan
@Ryan: Thank You!!!!! It's nice to know I'm not doing everything completely wrong!
Charmander
+1  A: 

I use jQuery (which uses an HTTP Request object under the covers). You would of course have to create a web service that it talks to, that does your database access. You should look at XML and JSON as alternatives to format the data you're passing.

Jay
@Jay: can you hook me up with an example?
Charmander
A: 

This sounds like the perfect task for a WCF Data Service. This basically lets jQuery talk directly (almost) to your database. Check out http://stephenwalther.com/blog/archive/2010/03/30/using-jquery-and-odata-to-insert-a-database-record.aspx for an example.

Mike Comstock