【发布时间】:2014-09-04 15:24:33
【问题描述】:
大家好, 在我的 Razor View 页面中,我有一个文本输入。我想通过使用“onchange”事件调用控制器并将我的输入值添加到我的 Razor 模型中包含的列表中。
这是我的html页面:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyForms</title>
</head>
<body>
<label>Code</label>
<form method="post" action="">
<input name="NPItem" maxlength="11" autofocus onkeypress="return isNumberKey(event)" onchange="location.href = '@Url.Action("addtoList", "MyController", new { formsData = Newtonsoft.Json.JsonConvert.SerializeObject(Model) })';"/>
</form>
<table border="1" id="listeNPAI">
<tr>
<th>Index</th>
<th>Value</th>
</tr>
@{
foreach (Tuple<string, string> item in Model.list) {
<tr>
<td>@item.Item1</td>
<td>@item.Item2</td>
</tr>
}
}
</table>
</body>
</html>
这是我调用的控制器动作:
public ActionResult addtoList(string formsData) {
formsData _form = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelClass>(formsData);
string input = Request["NPItem"];
if (input == null) { input = ""; } else { input = input.Trim(); }
if (input.Length == 11) {
_form.list.Add(new Tuple<string, string>(input.Substring(0, 5), input.Substring(6)));
}
return View("FormulaireNPAI", _form);
}
我将输入文本值添加到我的控制器操作中的列表中。问题是输入总是'==null'(因为没有提交)。但是,当我按下 Enter 键盘按钮时,它会起作用。
请帮忙 提前考虑
【问题讨论】:
-
我认为当您需要刷新模型然后更新视图时,您需要对服务器进行 AJAX 调用。
标签: c# javascript razor