找到你要的答案

Q:Bind a button to an action in controller and pass model from view to controller in MVC

Q:绑定一个按钮到控制器的一个动作,通过模型视图控制器在MVC

I'm trying to use jQuery AJAX to bind a button to an action in my controller. I do it this way, but it's not working.

@model InventoryTest.MyModel

@using (Html.BeginForm(new { id = "itemTypeForm" }))
{
    <input type="submit" value="Delete" id="deleteButton"  class="button btn btn-danger" />

    <!-- other form elements... -->
}

In controller I have:

[HttpPost]
public ActionResult Delete(MyModel myModel )
{
    if (myModel != null);
        // do something...

    return(view);
}

The AJAX code is:

$("#deleteButton").on('click', function (e) {
    e.preventDefault();
    if (formIsValid) {
        $.ajax({
            url: '/Home/Delete',
            type: 'POST',
            data: form.serialize()
        }).done(function (data) {
            if (data.success) {
                alert("sucsess")
            } else {
               alert("error");
            }
        });
    }
})

Edit:

$("#deleteButton").on('click', function (e) {
        e.preventDefault();
        $.ajax({
            url: '/Home/Delete',
            type: 'POST',
            **data:$("#itemTypeForm").serialize()**
        }).done(function (data) {
            if (data.success) {
                alert("sucsess")
            } else {
                alert("error");
            }
        });
    })

I changed AJAX part to access the form using its id,it now invoke the action but i get the error message and the returned model object is empty.

我想使用jQuery的Ajax绑定一个按钮在我的控制器的一个动作。我这样做,但它不工作。

@model InventoryTest.MyModel

@using (Html.BeginForm(new { id = "itemTypeForm" }))
{
    <input type="submit" value="Delete" id="deleteButton"  class="button btn btn-danger" />

    <!-- other form elements... -->
}

在控制器我有:

[HttpPost]
public ActionResult Delete(MyModel myModel )
{
    if (myModel != null);
        // do something...

    return(view);
}

Ajax代码:

$("#deleteButton").on('click', function (e) {
    e.preventDefault();
    if (formIsValid) {
        $.ajax({
            url: '/Home/Delete',
            type: 'POST',
            data: form.serialize()
        }).done(function (data) {
            if (data.success) {
                alert("sucsess")
            } else {
               alert("error");
            }
        });
    }
})

编辑:

$("#deleteButton").on('click', function (e) {
        e.preventDefault();
        $.ajax({
            url: '/Home/Delete',
            type: 'POST',
            **data:$("#itemTypeForm").serialize()**
        }).done(function (data) {
            if (data.success) {
                alert("sucsess")
            } else {
                alert("error");
            }
        });
    })

我改变了Ajax部分使用ID访问的形式,现在调用动作但我得到的错误信息并返回的模型对象是空的。

answer1: 回答1:

You should wrap the current Id in hidden element where name must match.

Say your delete id name in Model is- ProjectID

Then-

@using (Html.BeginForm(new { id = "itemTypeForm" }))
{
    @Html.HiddenFor(m=>m.ProjectID) //getting from above model
    <input type="submit" value="Delete" id="deleteButton"  class="button btn btn-danger" />

    <!-- other form elements... -->
}

And then-

[HttpPost]
public ActionResult Delete(MyModel myModel )
{
    int projectID = myModel.ProjectID  //just to make sure if id is coming
    if (myModel != null);
        // do something...

    return(view);
}

This way you can get it to work.

您应该将当前id隐藏在名称必须匹配的隐藏元素中。

说你删除ID的名字在模型- ProjectID

然后—

@using (Html.BeginForm(new { id = "itemTypeForm" }))
{
    @Html.HiddenFor(m=>m.ProjectID) //getting from above model
    <input type="submit" value="Delete" id="deleteButton"  class="button btn btn-danger" />

    <!-- other form elements... -->
}

然后—

[HttpPost]
public ActionResult Delete(MyModel myModel )
{
    int projectID = myModel.ProjectID  //just to make sure if id is coming
    if (myModel != null);
        // do something...

    return(view);
}

这样你就可以得到它的工作。

answer2: 回答2:

First of all you should return Json, not a view.

And in your Ajax call, set the expected data type to Json aswell.

"basic ajax tutorial"

首先你应该返回的JSON,不期。

在你的Ajax调用,设置数据类型的JSON和预期。

“基础教程”

jquery  ajax  asp.net-mvc  asp.net-mvc-4