I have this
View
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-inline", role = "form" }))
{
@Html.TextBox("SearchString", ViewBag.SearchFilter as string, new { @class = "form-control", onclick="keystroke()", id = "search" })
}
Controller
//GET: Home
public async Task<ActionResult> Index(MyViewModel model)
{
//...
if (model.SearchString != null)
{
//...
var myString = model.SearchString;
var b = model.FirstInt
}
//...
return View(model);
}
ViewModel
public class MyViewModel
{
//...
public string SearchString { get; set; }
public int? FirstInt { get; set; }
}
Javascript
function keystroke() {
var firstint = 1;
$("#search").keypress(function(event) {
if (event.which === 13) {
event.preventDefault();
$("form").submit(function() {
var text = $('#SearchString').val();
sendForm(firstint, text);
});
}
});
}
function sendForm(firstint, text) {
var data = {FirstInt: firstint, SearchString: text}
$.ajax({
url: "Home/Index",
type: 'POST',
data: data,
success: function () {
alert('success');
}
});
};
and its does not work.
I want to when user press the Enter key in search-input, I sent to controller the text which he entered, and the value of firstint. If I remove all javasript, then pressing Enter all normally sends to the controller but I need the firstint value too.
Indexcode you can log the contents ofmodel.