1

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)

2
  • Post it back to a controller method using ajax. Commented Oct 22, 2015 at 4:11
  • You can also store that FEN string value in hidden field and you will get that value in your controller method. Define method parameter with same name as hidden field name or you can get it from form collection. Commented Oct 22, 2015 at 4:38

1 Answer 1

1

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.

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

Comments

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.