MVC5 Ajax.ActionLink()可以工作,但Ajax.BeginForm()不能

时间:2015-02-04 12:11:37

标签: jquery twitter-bootstrap asp.net-mvc-5 microsoft-ajax

编辑 - 解决方案:问题是...控制器名称错字:

@using (Ajax.BeginForm("_AddNote", "Request", new AjaxOptions // controller should be "Requests"

说什么......

结束修改

我有一个我自己无法弄清楚的愚蠢问题。

我有bootstrap模式,其中我有Ajax表单。它没有提交。点击提交按钮后没有任何反应。

<a href="#AddNote-@item.RequestId" data-toggle="modal" data-target="#AddNote-@item.RequestId">
    <span class="glyphicon glyphicon-pencil"></span>
</a>
<div class="modal fade" id="AddNote-@item.RequestId" tabindex="-1" role="dialog" aria-labelledby="NoteLabel-@item.RequestId" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">@Ress.Ress.Close</span></button>
            <h4 class="modal-title text-center" id="NoteLabel-@item.RequestId">@Ress.Ress.AddNote</h4>
        </div>
        @using (Ajax.BeginForm("_AddNote", "Request", new AjaxOptions
        {
            UpdateTargetId = "Note-" + item.RequestId,
            HttpMethod = "Post",
            InsertionMode = InsertionMode.Replace
        }))
        {
            <div class="modal-body">
                <div class="row">
                    <div class="col-sm-10 col-lg-offset-1">
                        <input type="text" name="note" class="form-control" />
                        <input type="hidden" name="requestId" value="@item.RequestId" />
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">@Ress.Ress.Cancel</button>
                <input type="submit" class="btn btn-primary" value="@Ress.Ress.Add">
            </div>
        }
    </div>
</div>

并在控制器中

    [HttpPost]
    public ActionResult _AddNote(string note, int requestId)
    {
        if (!string.IsNullOrWhiteSpace(note) && requestId > 0)
        {
            string mes = _ifc.AddNote(requestId, note);
            return PartialView("_AddNoteResult", mes);
        }
        return PartialView("_Empty");
    }

要测试我是否使用了Ajax.ActionLink并且工作得很好。

 @Ajax.ActionLink("_AddNote", "_AddNote", new { requestId = 0 }, new AjaxOptions
                                {
                                    UpdateTargetId = "Note-" + item.RequestId,
                                    HttpMethod = "Post",
                                    InsertionMode = InsertionMode.Replace
                                }, new { @class = "btn" })

这不是不引人注目的,检查,ajax和验证。任何其他想法。除此之外,只有一个模态和DataPicker以及普通的Html.BeginForm(),但我不记得在使用这个组合之前有任何问题。

我到了墙上?

这是我的捆绑包:

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-2.1.1.min.js",
                    "~/Scripts/jquery.unobtrusive-ajax.min.js"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.min.js",
                  "~/Scripts/respond.min.js",
                  "~/Scripts/moment-with-locales.min.js",
                  "~/Scripts/bootstrap-datetimepicker.min.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate.min.js",
                    "~/Scripts/jquery.Validate.unobtrusive.min.js",
                    "~/Scripts/jquery.fix.decimal.js"));

这是我的额外阅读全文:

    @model IEnumerable<Ag.Areas.Agency.Models.Requests.ShowVM>
@{
    ViewBag.Title = Ress.Ress.TitleRequestShow;
}
@section Action {
    @*@Html.ActionLink(Ress.Ress.New, "New", null, new { @class = "btn btn-sm btn-primary" })*@
}
@using (Html.BeginForm("Show", "Requests"))
{
    <div class="row">
        <div class="col-sm-2">
            <div class="form-group">
                @MyHelpers.DateBox(ViewBag.DateFrom, "dateFrom", "form-control", "DateFrom")
            </div>
        </div>
        <div class="col-sm-2">
            <div class="form-group">
                @MyHelpers.DateBox(ViewBag.DateTo, "dateTo", "form-control", "DateTo")
            </div>
        </div>
        <div class="col-sm-2">
            <div class="form-group">
                @MyHelpers.DropDown(ViewBag.Partner, "partner", "form-control", null, false)
            </div>
        </div>
        <div class="col-sm-2">
            <div class="form-group">
                @MyHelpers.DropDown(ViewBag.StatusDropDown, "status", "form-control", null, false)
            </div>
        </div>
        <div class="col-sm-2">
            <div class="form-group">
                @MyHelpers.DropDown(ViewBag.PageSizeDropDown, "pageSize", "form-control", null, false)
            </div>
        </div>
        <div class="col-sm-2">
            <input type="hidden" name="sortBy" value="@ViewBag.SortBy" />
            <input type="hidden" name="isAsc" value="@ViewBag.IsAsc" />
            <input type="submit" value="@Ress.Ress.Filter" class="btn btn-default btn-sm" />
            @Html.ActionLink(Ress.Ress.Reset, "Show", null, new { @class = "btn btn-default btn-sm" })
        </div>
    </div>
}
<table class="table table-hover table-responsive">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.RequestId)
        </th>
        <th>
            @sortLink(Ress.Ress.ServiceId, "ServiceId") / @sortLink(Ress.Ress.ServiceTypeId, "ServiceTypeId")
        </th>
        <th>
            @sortLink(Ress.Ress.PickupDateTime, "PickupDateTime")
        </th>
        <th>
            @sortLink(Ress.Ress.StartId, "StartId")
        </th>
        <th>
            @sortLink(Ress.Ress.EndId, "EndId")
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Pax)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PersonName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RoomNo)
        </th>
        <th>
            @sortLink(Ress.Ress.Privat, "Privat")
        </th>
        <th>
            @sortLink(Ress.Ress.PaysFor, "PaysFor")
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExternalService)
        </th>
        <th>
            @Ress.Ress.Action
        </th>
    </tr>
    @foreach (var date in ViewBag.DateList)
    {
        string dateShow = date.ToString("dd.MM.yyyy");
        <tr>
            <td colspan="12">
                <div class="col-sm-4">
                    <h4>@dateShow - @String.Format("{0:dddd}", date)</h4>
                </div>

                <div class="col-sm-4 text-center ig-no-v-space">@pageLinks()</div>
            </td>
        </tr>

        foreach (var item in Model)
        {
            if (date.Date == item.PickupDateTime.Date)
            {
                string clas = "";
                if (item.Privat)
                {
                    clas = "success";
                }
                if (item.PaysFor)
                {
                    clas = "warning";
                }
                <tr class="@clas">
                    <td>
                        @Html.DisplayFor(modelItem => item.RequestId)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ServiceTypeName)
                        <br />
                        @Html.DisplayFor(modelItem => item.ServiceName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.PickupDateTime)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.StartName)
                    </td>
                    <td>
                        @if (item.InTheMiddle.Count() >= 1)
                        {
                            foreach (var midd in item.InTheMiddle)
                            {
                                <div>@Html.DisplayFor(modelItem => midd.PlaceName)</div>
                            }
                        }
                        @Html.DisplayFor(modelItem => item.EndName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Pax)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.PersonName)
                        <br />
                        @Html.DisplayFor(modelItem => item.PartnerFriendlyName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.RoomNo)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Privat)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.PaysFor)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ExternalService)
                    </td>
                    <td>
                        @if (item.PickupDateTime.Date >= DateTime.Today)
                        {

                            @Html.ActionLink(Ress.Ress.Edit, "EditSort", new { request = item.RequestId }, new { @class = "btn btn-default" })
                            <a data-toggle="modal" data-target="#CancelTransferRequest-@item.RequestId" class="btn btn-danger btn-xs">
                                <span class="glyphicon glyphicon-remove"></span>
                            </a>
                            <div class="modal fade" id="CancelTransferRequest-@item.RequestId" tabindex="-1" role="dialog" aria-labelledby="CancelLabel-@item.RequestId" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">@Ress.Ress.Close</span></button>
                                            <h4 class="modal-title text-center" id="CancelLabel-@item.RequestId">@Ress.Ress.StornoReason</h4>
                                        </div>
                                        @using (Html.BeginForm("_CancelRequest", "Requests", FormMethod.Post))
                                        {
                                            @Html.AntiForgeryToken()
                                            <div class="modal-body">
                                                <div class="row">
                                                    <div class="col-sm-6 col-lg-offset-3">
                                                        <textarea name="reason" rows="4" class="form-control"></textarea>
                                                        <input type="hidden" name="id" value="@item.RequestId" />
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">@Ress.Ress.Cancel</button>
                                                <button type="submit" class="btn btn-danger">@Ress.Ress.Storno</button>
                                            </div>
                                        }
                                    </div>
                                </div>
                            </div>
                        }
                        <a href="#AddNote-@item.RequestId" data-toggle="modal" data-target="#AddNote-@item.RequestId">
                            <span class="glyphicon glyphicon-pencil"></span>
                        </a>
                        <div class="modal fade" id="AddNote-@item.RequestId" tabindex="-1" role="dialog" aria-labelledby="NoteLabel-@item.RequestId" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">@Ress.Ress.Close</span></button>
                                        <h4 class="modal-title text-center" id="NoteLabel-@item.RequestId">@Ress.Ress.AddNote</h4>
                                    </div>
                                    @using (Ajax.BeginForm("_AddNote", "Request", new AjaxOptions
                                    {
                                        UpdateTargetId = "Note-" + item.RequestId,
                                        HttpMethod = "Post",
                                        InsertionMode = InsertionMode.Replace
                                    }))
                                    {
                                        <div class="modal-body">
                                            <div class="row">
                                                <div class="col-sm-10 col-lg-offset-1">
                                                    <input type="text" name="note" class="form-control" />
                                                    <input type="hidden" name="requestId" value="@item.RequestId" />
                                                </div>
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">@Ress.Ress.Cancel</button>
                                            <input type="submit" class="btn btn-primary" value="@Ress.Ress.Add">
                                        </div>
                                    }
                                </div>
                            </div>
                        </div>
                        @Ajax.ActionLink("_AddNote", "_AddNote", new { requestId = 0 }, new AjaxOptions
                                    {
                                        UpdateTargetId = "Note-" + item.RequestId,
                                        HttpMethod = "Post",
                                        InsertionMode = InsertionMode.Replace
                                    }, new { @class = "btn" })
                    </td>
                </tr>
                <tr class="alert-warning ig-invisible">
                    <td colspan="12" id="Note-@item.RequestId">
                        @if(!string.IsNullOrWhiteSpace(item.Note))
                        {
                            Html.RenderPartial("_AddNoteResult", (string)item.Note);
                        }
                    </td>
                </tr>
            }
        }
    }
</table>
<hr />
<div class="text-center">
    @pageLinks()
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $(function () {
            $('#DateFrom').datetimepicker({
                language: "hr",
                format: 'DD:MM:YYYY',
                pickTime: false,
            });
        });
        $(document).ready(function () {
            $(document).on('focus', '#DateTo', function () {
                $(this).datetimepicker({
                    language: "hr",
                    format: 'DD:MM:YYYY',
                    pickTime: false,
                });
            });
        });

    </script>
}

@helper buildLinks(int start, int end, string innerContent)
{
    <ul class="pagination">
        @for (int i = start; i <= end; i++)
        {
            string x = i == ViewBag.CurrentPage ? "active" : "";
            <li class="@x">@Html.ActionLink(@i.ToString(), "Show", 
                new {dateFrom = ViewBag.DateFrom, dateTo = ViewBag.DateTo,
                pageSize = ViewBag.PageSize, page = i, sortBy = ViewBag.SortBy, isAsc = ViewBag.IsAsc,
                partner = ViewBag.PartnerId, status = ViewBag.Status })</li>
        }
    </ul>

}
@helper pageLinks()
{
    const int maxPages = 11;

    if (ViewBag.TotalPages <= maxPages)
    {
        @buildLinks(1, (int)ViewBag.TotalPages, null)
        return;
    }

    int pagesAfter = ViewBag.TotalPages - ViewBag.CurrentPage; // Number of pages after current
    int pagesBefore = ViewBag.CurrentPage - 1; // Number of pages before current

    if (pagesAfter <= 4)
    {
        @buildLinks(1, 1, null) // Show 1st page

        int pageSubset = ViewBag.TotalPages - maxPages - 1 > 1 ? ViewBag.TotalPages - maxPages - 1 : 2;
        @buildLinks(pageSubset, pageSubset, "...") // Show page subset (...)

                                                @buildLinks(ViewBag.TotalPages - maxPages + 3, ViewBag.TotalPages, null) // Show last pages

        return; // Exit
    }

    if (pagesBefore <= 4)
    {
        @buildLinks(1, maxPages - 2, null) // Show 1st pages


        int pageSubset = maxPages + 2 < ViewBag.TotalPages ? maxPages + 2 : ViewBag.TotalPages - 1;
        @buildLinks(pageSubset, pageSubset, "...") // Show page subset (...)

                                                @buildLinks(ViewBag.TotalPages, ViewBag.TotalPages, null) // Show last page

        return; // Exit

    }

    if (pagesAfter > 4)
    {
        @buildLinks(1, 1, null) // Show 1st pages

        int pageSubset1 = ViewBag.CurrentPage - 7 > 1 ? ViewBag.CurrentPage - 7 : 2;
        int pageSubset2 = ViewBag.CurrentPage + 7 < ViewBag.TotalPages ? ViewBag.CurrentPage + 7 : ViewBag.TotalPages - 1;

        @buildLinks(pageSubset1, pageSubset1, pageSubset1 == ViewBag.CurrentPage - 4 ? null : "...") // Show 1st page subset (...)

                                                                                                    @buildLinks(ViewBag.CurrentPage - 3, ViewBag.CurrentPage + 3, null) // Show middle pages

        // Show 2nd page subset (...)
        // only show ... if page is contigous to the previous one.
        @buildLinks(pageSubset2, pageSubset2, pageSubset2 == ViewBag.CurrentPage + 4 ? null : "...")
        @buildLinks(ViewBag.TotalPages, ViewBag.TotalPages, null) // Show last page

        return; // Exit

    }
}
@helper sortLink(string name, string sortBy)
{
    @Html.ActionLink(@name, "Show", new
    {
        dateFrom = ViewBag.DateFrom,
        dateTo = ViewBag.DateTo,
        pageSize = ViewBag.PageSize,
        page = ViewBag.CurrentPage,
        sortBy = sortBy,
        isAsc = (sortBy == ViewBag.SortBy ? !ViewBag.IsAsc : ViewBag.IsAsc),
        partner = ViewBag.PartnerId,
        status = ViewBag.Status
    })
}

0 个答案:

没有答案