ive MVC 5应用程序,在索引页面中你有一个表,如何从索引页面调用控制器中的创建操作而不是默认按钮。 默认按钮就像
一样@Html.ActionLink("Create New", "Create")
但是我的表格有点不同 我使用以下按钮
<input type="button" value="Add" onclick="add()" class="data-button" id="add-row" />
如何通过此按钮调用创建操作?
答案 0 :(得分:1)
首先,将“创建”按钮设为id
,以便轻松识别:
@Html.ActionLink("Create New", "Create", null, new { id = "create-button" })
然后点击Add
按钮,从该按钮获取href
并重定向页面:
function add() {
window.location.assign($('#create-button').prop('href'));
}
如果您希望将事件附加到jQuery中的Add
按钮,请尝试以下操作:
<input type="button" value="Add" class="data-button" id="add-row" />
$('#add-row').click(function() {
window.location.assign($('#create-button').prop('href'));
});
答案 1 :(得分:1)
使用输入按钮调用动作方法有很多种方法。我已经添加了两个,如下所示。
1.您可以使用javascript调用该操作。您可以添加以下javascript函数。
<input type="button" value="Add" onclick="add()" class="data-button" id="add-row" /> function add() { window.location = @Url.Action("Create"); }
2.使用表单元素。如果您将按钮放在某个表单中,那么当用户单击按钮时,它将在指定的URL上发布。
@using (Html.BeginForm("Create")) { <input type="button" value="Add" onclick="add()" class="data-button" id="add-row" /> }
答案 2 :(得分:1)
你必须这样做才能调用它:
@Html.ActionLink("Create New", "Create", null, new { id = "linkCreate" })
<input type="button" value="Add" class="data-button" id="add-row" />
您必须编写按钮单击事件并在其功能中使用click()
调用该链接$(document).ready(function(){
$("#add-row").click(function(event){
$("#linkCreate").click();
event.preventDefault();
});
});
答案 3 :(得分:0)
或者您可以像这样使用$ .Post
$('#add-row').click(function(){
// data is the values that you are passing to the controller
// '/Index/Create' is the Url of the action - Area/Controller/Action
$.post('/Index/Create',data,function(){
// This is the code for success function
alert("new record has been added successfully");
});
});