【发布时间】:2015-08-08 10:50:58
【问题描述】:
我有这个
查看
@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" })
}
控制器
//GET: Home
public async Task<ActionResult> Index(MyViewModel model)
{
//...
if (model.SearchString != null)
{
//...
var myString = model.SearchString;
var b = model.FirstInt
}
//...
return View(model);
}
视图模型
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');
}
});
};
它不起作用。
我想当用户在搜索输入中按下 Enter 键时,我将他输入的文本和 firstint 的值发送给控制器。如果我删除所有 javasript,然后按 Enter all 通常会发送到控制器,但我也需要 firstint 值。
【问题讨论】:
-
控制器接收什么数据?我假设在
Index代码中你可以记录model的内容。 -
用户在输入字段中输入一个字符串。我正在向控制器发送一个字符串和一些数字。这个数字是在用户按下一个键时计算的。我怎样才能做到这一点?观看此stackoverflow.com/questions/30459261/…@HewWolff
-
你也可以试试@Ajax.BeginForm 并声明你的js函数
-
你能写一个可行的解决方案吗? @Shammelburg
标签: javascript c# jquery ajax asp.net-mvc