View values submit to controller with post

Asked

Viewed 20 times

-1

I am quite beginner in MVC and was here as a question: In view I have:

<form id="form1" action="/Jogo/ExecutarAcao" method="post">
 <input type="radio" id="banana" name="opcao" value="banana" onclick="this.RespostaDoJogador.submit();">
    <label for="banana">Banana</label><br>
    <input type="radio" id="laranja" name="opcao" value="laranja" onclick="this.RespostaDoJogador.submit();">
    <label for="laranja">Laranja</label><br>
    <input type="radio" id="melao" name="opcao" value="melao" onclick="this.RespostaDoJogador.submit();">
    <label for="melao">Melão</label>
    <input type="radio" id="tomate" name="opcao" value="tomate" onclick="this.RespostaDoJogador.submit();">
    <label for="tomate">Tomate</label>
    <button type="submit" form="form1" value="submit">submit</button>
</form>

And in Controller I have:

[HttpPost]
public IActionResult ExecutarAcao(int idJogo, string playerAction, int player1x, int player1y, int player2x, int player2y, int player3x, int player3y, int player4x, int player4y)

{
    Jogo jogoAtual = Repository.CarregarJogo(idJogo);
    jogoAtual.ExecutarAcao(playerAction, player1x, player1y, player2x, player2y, player3x, player3y, player4x, player4y);
    return View("Prototipo", jogoAtual);
}

And my goal is: The value that is submitted should be the value attributed to the property RespostaDoJogador, which is a property of Model Game.

2 answers

0

You should at least say which software we are dealing with or if it is pure even, but the problem there is that Onclick prevents sending the form directly, if you want to do this client-side check, you should finish the service in the javascript itself. Onclick is suitable for this type of approach now, I think if the system requires prior verification, it should be done on the server side, plus you who knows.

Do the pertinent check and finish making the request with ajax, now this is in html, I do not know what you are building in C# that will need html, so I will not even suggest code, better analyze and see if it can not replace itlo by PHP which is a universal language, easy to understand, you will have greater support to solve problems, C# is not difficult anymore people seek to learn C# when they need to work with more complex things like building a kernel, I don’t have that need.

Post request with javascript: https://www.devmedia.com.br/ajax-com-jquery-trabalhando-com-requisicoes-assincronas/37141

Example of ajax

jQuery.ajax method In the jQuery library, one of the most commonly used functions is $.ajax(), which, with a very simple syntax, allows sending and handling the result of asynchronous requests. In List 1, we have a basic example of using this method, in which we send some information via POST to a PHP file.

$.ajax({
 method: "POST",
 url: "cadastrar.php",
 data: { nome: "Pedro", email: "[email protected]" }
})

Jquery example

jQuery.post method The $.post() function is also a simplified form of $.ajax(), however, this time the requests are sent by middle of the HTTP POST method. In List 3 we have an example of use.

$.post("salvar_dados.php", {
    nome : "Maria Fernanda", salario : "3000"
}, function(msg){
    $("#resultado").html(msg);
})

0

You could remove the onclick="this.RespostaDoJogador.submit(); other view components and only put him on button, after that you could create a vector in which you would catch all the input radios, example:

var arrayDeRadios = document.getElementsByTagName('input').value;

And then you could do a check using switch or if, that way:

if(arrayDeRadios[0].checked()){
console.log('O usuário escolheu Banana!');
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.