I'm a nube, so my question can be simple, but I'm already stuck for a two days because of it. So, I have an html form, in which client can leave their contact data like
<form>
<input id="name" type="text" placeholder="Имя"><br>
<input id="Email" type="text" placeholder="E-mail"><br>
<input id="phonenumber" type="text" placeholder="Телефон"><br>
<input id="adress" type="text" placeholder="Адрес"><br>
<textarea id="comment" placeholder="Комментарий"></textarea>
<button id="submit" class="send" value="Отправить">Отправить</button>
</form>
And I wish to catch it on my controller
[HttpPost]
public ActionResult setupRequest(AirconditioningSetupRequestModel model)
{
//model goes to DB
return View();
}
How can I deal with that with AJAX? Help me please.
Use something like this: string name=Request.Form["name"]; etc...
//Save Click Function
var Name=$("#name").val();
var Email=$("#Email").val();
$.ajax({
url:'ControllerName/Save',
type:'POST',
data:{ContName: Name, ContEmail:Email}
success: function(data)
{
//data return from controller
}
});
public ActionResult Save(string ContName, string ContEmail)
{
//Save to DB
}
You can set method and action attribute for form
method - post action - controller/setupRequest
Since you have form and input submit this will trigger your action in MVC controller. However to make use of default model binding set name attributes for input elements as used in your model AirconditioningSetupRequestModel which will be binded in server side on form submit.