C#ActionLink MVC4-将多个参数传递给ActionResult
--对C#和MVC来说非常陌生-- 在我的视图/Tasks/Index.aspx中,我将以下行/单元格包装在一个表中C#ActionLink MVC4-将多个参数传递给ActionResult,c#,asp.net-mvc,asp.net-mvc-4,actionlink,actionresult,C#,Asp.net Mvc,Asp.net Mvc 4,Actionlink,Actionresult,--对C#和MVC来说非常陌生-- 在我的视图/Tasks/Index.aspx中,我将以下行/单元格包装在一个表中 <tr> <td><%: Html.ActionLink( HttpUtility.HtmlDecode("+"), "Insert", "Tasks", new { o
<tr>
<td><%:
Html.ActionLink(
HttpUtility.HtmlDecode("+"),
"Insert",
"Tasks",
new { onclick = "InsertTask();" },
new { @class = "ActionButton AddButton" }
)
%></td>
<td><input type="text" id="txtAddName" style="width: 200px;" /></td>
<td><input type="text" id="txtAddDescription" style="width: 400px;"/></td>
<td><input type="text" id="txtAddStarting" style="width: 200px;" /></td>
<td><input type="text" id="txtAddEnding" style="width: 200px;" /></td>
</tr>
<% if (ViewBag.Message != null) { %>
<%: ViewBag.Message %>
<% } %>
<script type="text/javascript">
function InsertTask() {
var name = $('#txtAddName').val();
var desc = $('#txtAddDescription').val();
var starting = $('#txtAddStarting').val();
var ending = $('#txtAddEnding').val();
$.ajax({
url: "Insert/" + name + "," + desc + "," + starting + "," + ending,
context: document.body
}).done(function () {
alert("done!");
});
}
</script>
当我点击ActionLink元素时,它
编辑
从SLaks response,我已将我的ActionLink更改为以下代码。。。并在我的JS函数末尾添加了一个“return false;”
<td><%:
Html.ActionLink(
HttpUtility.HtmlDecode("+"),
"Insert",
"Tasks",
null,
new { onclick = "InsertTask();" , @class = "ActionButton AddButton" }
)
%></td>
它现在调用.CS控制器ActionResponse方法。。。但是收到的所有参数都是空的。您需要从内联处理程序返回false,以防止浏览器执行默认操作(导航到链接)如果您将ajax调用更改为此,则它应该可以工作:
$.ajax({
url: "Tasks/Insert",
data: { name: name, description: desc, starting: starting, ending: ending },
context: document.body
}).done(function () {
alert("done!");
});
我感谢你的反馈。。。但正如前面提到的,对于C#和MVC来说是非常新的。我完全不知道内联处理程序会在哪里。我相信这只解决了一个问题,即整页重新加载?@adam:`onclick=“InsertTask();”``是内联处理程序,但它位于错误的位置。看看生成的源代码,你就会明白我的意思。谢谢你,这让我走上了正确的方向。我将发布当前代码的更新到我的OP。它现在正在调用函数,但ActionResult中的任何参数都没有值。ActionURL不是这样工作的。您需要创建一个与路由匹配的URL,或者创建一个查询字符串。将
数据
参数传递给jQuery将为您构建一个查询字符串。顺便说一句,您只需在源代码中编写+
,而无需调用HtmlDecode
。我尝试过了,但我的程序拒绝了我尝试的每种格式+("+") -- ("++") -- "++" .. 等等……不;我指的是文字字符+
。在C#中不需要HTML转义;你可以写“+”
。哦,明白了。我使用的角色稍有不同,而且更大胆=P+是您正在使用的字符。您可以直接在源代码中编写它。
$.ajax({
url: "Tasks/Insert",
data: { name: name, description: desc, starting: starting, ending: ending },
context: document.body
}).done(function () {
alert("done!");
});