如何使用Razor MVC 5.2.3 Ajax的多个提交按钮

时间:2016-05-09 19:58:03

标签: javascript ajax asp.net-mvc razor twitter-bootstrap-3

我有一个通过ajax调用局部视图的视图。局部视图有一个表,其中包含另一个ajax形式。每行包含两个提交按钮,我希望将其映射到两个不同的操作:保存和删除。但是,当我提交任一按钮时,没有数据传递给控制器​​。 ProjectComment projectcomment@model MyProject.Areas.MyComments.Models.ProjectComment @using (Ajax.BeginForm("Details", null, new AjaxOptions { HttpMethod = "POST", // HttpPost InsertionMode = InsertionMode.Replace, // empty the target first UpdateTargetId = "commentTbl" // place content within #commentTbl }, new { @class = "form-inline" })) { @Html.AntiForgeryToken() <p>...</p> <div class="ibox float-e-margins">...</div> } <div id="commentTbl">...Partial View is loaded here...</div> 不包含表单中的数据注释数据。我有以下代码:

MyView的:

Index.cshtml

@model IEnumerable<MyProject.Areas.MyComments.Models.ProjectComment>

@using (Ajax.BeginForm("Update", null,
new AjaxOptions
{
    HttpMethod = "POST", // HttpPost
    InsertionMode = InsertionMode.Replace, // empty the target first
    UpdateTargetId = "commentTbl" // place content within #commentTbl
}, new { @class = "form-inline" }))
{
<div class="ibox float-e-margins">
    <div class="ibox-title">...</div>
    <div class="ibox-content">
        <div class="table-responsive">
            <table class="table table-striped table-bordered table-hover dataTables-example" id="dataTables-comments">
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.Comment)
                        </th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @{int i = 0;}
                    @foreach (var projectcomment in Model)
                    {
                        <tr>
                            <td>
                                @Html.EditorFor(modelItem => projectcomment.Comment)
                            </td>
                            <td>
                                <button type="submit" id="save" class="btn btn-primary" name="Save" value='@i'>Save</button>
                                <button type="submit" id="delete" class="btn btn-primary" name="Delete" value='@i'>Delete</button>
                            </td>
                        </tr>
                        i++;
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
}

部分视图

Details.cshtml

[HttpPost]
public ActionResult Update(ProjectComment projectcomment, String submitAction)
{
    // submitAction is null and projectcomment contains no form data.
    if (submitAction.Equals("Save"))   
        return RedirectToAction("Save", projectcomment);
    else if (submitAction.Equals("Delete"))
        return RedirectToAction("Delete", projectcomment);
    else
        return HttpNotFound();
}

[HttpPost]
public ActionResult Save([Bind(Include="Comment")] ProjectComment projectcomment)
{
   // ... to do logic here
}

我的控制器

CommentController.cs

public function getSearchQuery($search_phrase) {
    return Car::select("car_models.id", "car_models.name", "brands.name")
        ->join("car_models", "cars.car_model_id", "=", "car_models.id")
        ->join("brands", "car_models.brand_id", "=", "brands.id")
        ->get();
}

请帮助我做错了。

1 个答案:

答案 0 :(得分:3)

修改您的按钮名称以匹配您的参数名称。例如:

您的密码:

<button type="submit" id="save" class="btn btn-primary" name="Save" value='@i'>Save</button>
<button type="submit" id="delete" class="btn btn-primary" name="Delete" value='@i'>Delete</button>

应该是什么:

<button type="submit" id="save" class="btn btn-primary" name="submitAction" value='Save'>Save</button>
<button type="submit" id="delete" class="btn btn-primary" name="submitAction" value='Delete'>Delete</button>

我修改了名称以匹配您在控制器中接受的参数名称和值。提交表单时,您现在将值传递给参数。