hildk
hildk

Reputation: 159

Updating database row in MVC without form controls

My view has a chessboard.js chess board. The user makes one move and then clicks a submit button. I want my HttpPost method to retrieve this new board position and update it in my database.

I'm new to MVC and all previous examples that I have seen have utilized values entered into controls to update databases. My question is how do I update a row in a database when the user doesn't enter any values into a form control - they just make a move on a chessboard? (Once the player makes a move a FEN string is updated with the new board position. I just need to get that string into my database once they press the submit button)

Upvotes: 1

Views: 274

Answers (1)

Andrei
Andrei

Reputation: 44550

If you're designing a chess game platform you should consider using SignalR if you need real-time chessboard updates. It will be a two-way communication with minimum latency. It's obviously not a basic topic for ASP.NET MVC beginners. There are a lot of materials on this here.


If you don't need real-time updates and you just want to send your move information to the server without any button submission, you can use javascript/jQuery and Ajax for it. Basically when user makes a move, javascript function should be called:

function sendMoveInformation(fen) {
    $.ajax({
        type: "POST",
        url: '/Game/MakeMove',
        data: "fen=" + fen,
        success: function () { /* do you need to do anything on success? */ }
    });
}

And your ASP.NET MVC controller action:

[HttpPost]
public ActionResult MakeMove(string fen)
{
    // do something with FEN
    return Content("OK");
}

Consider using ASP.NET WebAPI and communication using JSON message format.

Upvotes: 1

Related Questions