0

我是 ASP.NET Core 的新手,这就是我从 Ajax 获取数据的方式。

我有一个像这样的视图模型

public class RequestSearchDefaultModel
{
    public string FromToDate { get; set; }
    public int Status { get; set; }
    public List<SelectListItem> StatusType { get; set; }
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public IEnumerable<RequestViewModel> requestViewModel { get; set; }
}

当我搜索表单时,我使用 Ajax 来获取数据

function OpenSearch() {
    var stt = $('#StatusDropDown').val();
    var daterange = $('#daterange').val();
    var url = "@Url.Action("Search","RequestApproval")";
    var model = { FromToDate: daterange, Status: stt };
    $.ajax({
        type: "POST",
        data: JSON.stringify(model),
        url: url,
        contentType: "application/json",
    }).done(function (res) {
        console.log("here");
        $('#myTable').html(res);
    })
}

这是控制器

[HttpPost]
public async Task<IActionResult> Search([FromBody] RequestSearchDefaultModel m)
{
    m.FromDate = DateTime.Parse(m.FromToDate.Substring(0, 10)).Add(new TimeSpan(00, 0, 0));
    m.ToDate = DateTime.Parse(m.FromToDate.Substring(14)).Add(new TimeSpan(23, 59, 59));
    var request = _headerService.Search();
    m.requestViewModel = _mapper.Map<IEnumerable<RequestViewModel>>(request);
    return PartialView("_RequestBody", m);
}

这是视图

<div class="card-body">
    <div class="table-responsive" id="myTable">
        @Html.Partial("_RequestBody")
    </div>
</div>

这是部分观点

@model RequestSearchDefaultModel

<table class="table table-striped">
    <thead>
        <tr>
            <th>Seq.</th>
            <th>Title</th>
            <th>Status</th>
            <th>Draft Date</th>
            <th>Final Approval Date</th>
        </tr>
    </thead>
    <tbody id="RequestBody">
        @if (Model.requestViewModel == null)
        {
        }
        else
        {
            foreach (var item in Model.requestViewModel)
            {
                <tr>
                    <td>@item.ID</td>
                    <td>@item.DocumentNo</td>
                    <td>
                        <a asp-action="Edit" asp-route-id="@item.ID">@item.Title</a>
                    </td>
                    <td>@item.DraftDate</td>

                </tr>
            }
        }
    </tbody>
</table>

但是我无法获取数据,我不知道我错在哪里,所以请帮助。提前致谢。

4

0 回答 0