JUN
17

I need to get into the habbit of posting more to my blog. As such, I thought I'd make a short post about how to do simple Ajax calls in MVC to get data from the server. Before continuing, if you're not familiar with MVC and JQuery, I'd suggest working on those skills first.

We're going to be using a SQL database for this example, along with the Entity Framework as our ORM.

What you'll need

  1. Visual Studio with some version of MVC installed
  2. SQL Server 2008 or above
  3. JQuery set up
  4. JQuery UI set up
  5. JQuery UI Dialog plugin installed

Sample Database

We'll use a simple database composed of Items and Categories. Items belong in zero or more more categories. Our database will look like this:

 

Relevant Controller Actions

First we start with a base controller, so all other controllers can use common resources.

using FunWithAjax.Models;

namespace FunWithAjax.Controllers
{
    public class FunWithAjaxController : Controller
    {
        protected FunAjaxEntities db = new FunAjaxEntities();
    }
}

Our Item controller's Index action will have IEnumerable<Item> pass to it's view.

namespace FunWithAjax.Controllers
{
    public class ItemController : FunWithAjaxController
    {
        public ActionResult Index()
        {
            return View(db.Items.ToArray());
        }
    }
}

Our Item view will have a fairly straight-forward listing of all items in the db.

 

Notice we added an extra attribute "data-item-id". This will be important later. Now when we compile, we get a list of items:

In our Home controller, we have the action that returns the partial view we want to render when looking at a specific item.

        [HttpGet]
        public ActionResult Jump(int? itemID)
        {
            return Jump(itemID, null);
        }

        [HttpPost]
        public ActionResult Jump(int? itemID, FormCollection formCollection)
        {
            Item item = db.Items.Where(i => i.ID == itemID).FirstOrDefault();
            if (formCollection == null)
            {
                return PartialView("JumpModal", item);
            }

            return View();
        }

And our partial view for the item details:

 

Finally, the Ajax request:

$(document).ready(function () {

    $('a#jump-link').click(function () {
        var itemID = $(this).attr('data-item-id');
        var itemName = $(this).html();
        $('#dialog').dialog({
            title: itemName,
            autoOpen: true,
            width: 400,
            height: 500,
            modal: true,
            open: function (event, ui) {
                $(this).load("/Home/Jump?itemID=" + itemID);
            },
            buttons: {
                "Close": function () {
                    $(this).dialog("close");
                }
            }
        });
    });

});

This reads like the following: When clicking a link, grab the id and name from the element, then use the dialog function to open a dialog. The key part is open function. Here we want to load directly from the details link.

What we end up with is much nicer than refreshing the page when we want to look at item details!

MAY
11

There are a ton of exciting developments in Windows 8.1 and Windows Phone, especially the Universal Apps features in Visual Studio. Definitely worth checking out the upcoming developments in VS 2013.

Part 1

Part 2

AUG
21

Okay, this is cool. I use Git for all my personal projects because it's free and easy to use. This addition called Ungit makes it even easier to manage git without using the command line. Check it out

APR
15

Minecraft is the ultimate sandbox auto-generated open-world awesomefest. It's packed with stuff to do, and yet demands nothing from the player (except to not die in survival mode). There are no quests. There are no rules. You literally are dropped in a world with nothing and you must figure everything out on your own (or look it up on the wiki).

There are plenty of tutorials online that show you how to set up a Minecraft Server on the Mac, but I could not find one that starts from initial setup of the server to making it available to the world. So that's what this is all about.

Starting the Server

This is the easy bit, but it's important to at least know the terminal command to start up the server.

  1. Check your installation of Java. It should be installed automatically by Apple Updates, but to check, open up a Terminal window (Terminal is in Applications->Utilities) and type 'java -version' (without quotes). If it is installed, it will show you the version info. If not, use Apple Update to update your Mac or download it online if possible.
  2. Go to http://minecraft.net/download and download the server (minecraft_server.jar).
  3. Find a location on your machine to store the server (e.g. ~/Desktop) and create a folder there called 'Minecraft Server' or whatever. Move the minecraft_server.jar into the newly created folder.
  4. To start the server, go to a Terminal window and navigate to its location (a la ~/Desktop/Minecraft\ Server), and type the following command: 'java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui'. To give you an idea of what that does, it's executing the java command which executes the minecraft_server.jar. The -Xmx1024M and -Xms1024M are allocating memory to be used by the server (it's not dynamic), the -jar signifies that it's a jar file, and the nogui is an input to the server saying you'll just use the terminal for log info, thank you very much.
After the server has started successfully, a slew of files will be dumped to the location. This is normal.

Publishing the Server

This part can be tricky because all routers are different. Regardless, I hope this will provide a little more insight than what you'll find elsewhere on the net.

Setting up the router

  1. Go to your router settings. Typically to do this, open a web browser and go to 192.168.1.1. If you're setup to use WPA/WPA2 (password protected), you'll need to give your login credentials.
  2. Once in the router settings, there should be a section called 'Port Forwarding'. In mine, it's in the supersection 'Applications and Gaming' and it's partitioned by Single and Range- Port Forwarding. In this case, we can use a single port.
  3. In this section, add an entry for Minecraft at External and Internal Port 25565 (the default for minecraft server) with an 'Either' or 'Both' for the network protocol. Assign a local IP address such as 192.168.1.11 in the entry. In mine, you need to check the 'Enabled' checkbox. Save the settings.

Setting up the machine's NIC

Go to System Preferences->Network. Click on the Advanced button. In the TCP/IP section, switch the 'Configure IPv4' to 'Manually'. Now set the IPv4 Address to the address we provided earlier (e.g. 192.168.1.11). Make sure to click Ok and then Apply.

At this point, you'll need to restart the server, and you should be done! I ran into one issue which will I'll address in the section below.

Issues

One issue I ran into was that whenever folks tried to join the server, they would get an error:

To fix this, open up the 'server.properties' file that was created in the server folder. Set the online mode to 'false' (online-mode=false).

Next Page >