将HTML表数据和模型数据传递到控制器MVC

时间:2019-07-18 12:49:01

标签: jquery asp.net-mvc

我正在尝试使用JQuery将控件中的数据(下拉列表,文本区域等)以及表中的数据传递给控制器​​,但是遇到了以下问题:

  1. 使用POST,不会将任何内容发送到我的控制器,但是使用GET可以。
  2. 使用LIST <>时,使用GET传递我的数据但不传递我的表数据,但是列表中什么都没有返回
  3. 使用Array作为参数时,它会返回一个丑陋的字符串,如格式,如果不做大量修改,我将无法使用。

所以我的问题是,为什么POST不起作用?使用GET时,为什么数据没有传递到我的List <>中?

下面是我的代码:

查看:

<div class="form-horizontal">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                            <div class="form-group">
                                @Html.LabelFor(model => model.LocumId, htmlAttributes: new { @class = "control-label col-md-4" })
                                <div class="col-md-8">
                                    @Html.Label(Model.Locum.FullName + " (" + Model.Locum.LocumCategory.LocumCategoryName + ")", htmlAttributes: new { @class = "control-label pointer", style = "font-weight: normal;cursor:pointer", data_target = "#basic", data_toggle = "modal" })
                                    @*<button id="btnViewLocumDetails" class="btn btn-sm btn-info" type="button" data-target="#basic" data-toggle="modal" style="width: 90px;margin-left:10px">Show details</button>*@
                                </div>

                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.JobPostingId, htmlAttributes: new { @class = "control-label col-md-4" })

                                @if (Model.JobPostings != null)
                                {
                                    <div class="col-md-8">
                                        <select id="JobPostingId" name="JobPostingId" class="selectpicker show-tick form-control" data-live-search="true">
                                            @foreach (var item in Model.JobPostings)
                                            {
                                                if (item.JobPostingTitle == Model.Locum.LocumCategory.LocumCategoryName)
                                                {
                                                    <option value="@item.JobPostingId" selected>@item.JobPostingTitle</option>
                                                }
                                                else
                                                {
                                                    <option value="@item.JobPostingId">@item.JobPostingTitle</option>
                                                }

                                            }
                                        </select>
                                        @Html.ValidationMessageFor(model => model.JobPostingId, "", new { @class = "text-danger" })
                                    </div>
                                    @*<div class="col-md-2">
                                        <button id="btnViewLocumDetails" class="btn btn-sm btn-info" type="button" onclick="location.href='@Url.Action("MyList","JobPostings")';return false;" style="width: 90px;">My postings</button>
                                    </div>*@
                                }
                                else
                                {
                                    <div class="col-md-8">
                                        @Html.Label(Model.JobPosting.JobPostingTitle + " (" + Model.Locum.LocumCategory.LocumCategoryName + ")", htmlAttributes: new { @class = "control-label", style = "font-weight: normal;" })
                                        @Html.HiddenFor(model => model.JobPostingId)
                                        <button id="btnViewJobPostingDetails" class="btn btn-sm btn-info" type="button" data-target="#basic" data-toggle="modal" style="width: 90px;margin-left:10px">Show details</button>
                                    </div>

                                }
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.BookingRate, htmlAttributes: new { @class = "control-label col-md-4" })
                                <div class="col-md-4">
                                    @Html.EditorFor(model => model.BookingRate, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.BookingRate, "", new { @class = "text-danger" })
                                </div>

                            </div>


                            @if (Model.Messages != null)
                            {
                                @Html.Partial("~/Views/Messages/_ListMessages.cshtml", Model.Messages.ToList())
                            }

                            @if (Model.BookingCreatedBy == "Practice")
                            {
                                <div class="form-group">
                                    @Html.LabelFor(model => model.BookingMessage, htmlAttributes: new { @class = "control-label col-sm-4" })
                                    <div class="col-md-8">
                                        @Html.TextAreaFor(model => model.BookingMessage, new { @class = "form-control", @placeholder = "Type a message for the locum you are sending the request to", @rows = "2" })
                                        @Html.ValidationMessageFor(model => model.BookingMessage, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            }

                            @if (Model.BookingCreatedBy == "Locum")
                            {
                                <div class="form-group">
                                    @Html.LabelFor(model => model.BookingMessage, htmlAttributes: new { @class = "control-label col-sm-4" })
                                    <div class="col-md-8">
                                        @Html.TextAreaFor(model => model.BookingMessage, new { @class = "form-control", @placeholder = "Type a message for the job owner you are sending the request to", @rows = "2" })
                                        @Html.ValidationMessageFor(model => model.BookingMessage, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            }

                            <div class="col-sm-12 text-center">
                                <button type="button" id="bookingaction" name="bookingaction" value="create" class="btn btn-info" style="width: 150px;">
                                    Send Request
                                </button>
                                @if (TempData["userType"].ToString() == "Practice" && Model.BookingCreatedBy == "Locum" && Model.BookingStatusId != 6)
                                {
                                    <button type="button" id="bookingaction" name="bookingaction" value="practice_accepted" class="btn btn-success" style="width: 150px;">
                                        Accept
                                    </button>
                                }
                                @if (TempData["userType"].ToString() == "Locum" && Model.BookingCreatedBy == "Practice" && Model.BookingStatusId != 6)
                                {
                                    <button type="button" id="bookingaction" name="bookingaction" value="locum_accepted" class="btn btn-success" style="width: 150px;">
                                        Accept
                                    </button>
                                }
                                <button type="button" class="btn btn-danger" autocomplete="off" onclick="location.href='@Url.Action("Index","Bookings")';return false;" style="width: 150px;">
                                    Cancel
                                </button>
                            </div>
                        </div>

<div class="form-horizontal">
                            <table id="dt_basicA" class="table table-borderless" width="100%" style="font-size:10px;">
                                <thead style="border-bottom:0px !important">
                                    <tr>
                                        <th data-hide="phone,tablet"></th>
                                        <th data-hide="expand">Date</th>
                                        <th data-hide="phone,tablet">Day of Week</th>
                                        <th data-class="phone,tablet"><i class="text-muted hidden-md hidden-sm hidden-xs"></i> Start Time</th>
                                        <th data-hide="phone,tablet"><i class="text-muted hidden-md hidden-sm hidden-xs"></i> End Time</th>
                                        <th data-class="phone,tablet"><i class="text-muted hidden-md hidden-sm hidden-xs"></i> Start Time</th>
                                        <th data-hide="phone,tablet"><i class="text-muted hidden-md hidden-sm hidden-xs"></i> End Time</th>
                                        @*<th data-hide="phone,tablet" style="white-space: nowrap;"><i class="fa fa-fw fa-calendar txt-color-blue hidden-md hidden-sm hidden-xs"></i> Start Date</th>
                    <th data-hide="phone,tablet" style="white-space: nowrap;"><i class="fa fa-fw fa-calendar txt-color-blue hidden-md hidden-sm hidden-xs"></i> End Date</th>*@
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in Model.BookingEvents.OrderBy(x=>x.StartDate))
                                    {
                                        <tr>
                                            <td><input type="checkbox" /></td>
                                            <td style="font-size:small">@item.StartDate</td>
                                            <td style="font-size:small">@item.DayOfTheWeek</td>
                                            <td style="font-size:small">@item.StartTime</td>
                                            <td style="font-size:small">@item.EndTime</td>
                                            <td>
                                                <select id="StartTime" name="StartTime" class="selectpicker show-tick form-control" title="Choose one...">
                                                    @foreach (SelectListItem itemStartTime in Model.StartTimeList)
                                                    {
                                                        if (itemStartTime.Text == "08:00")
                                                        {
                                                            <option value="@itemStartTime.Value" selected>@itemStartTime.Text</option>
                                                        }
                                                        else
                                                        {
                                                            <option value="@itemStartTime.Value">@itemStartTime.Text</option>
                                                        }

                                                    }
                                                </select>

                                            </td>
                                            <td>
                                                <select id="EndTime" name="EndTime" class="selectpicker show-tick form-control" title="Choose one...">
                                                    @foreach (SelectListItem itemEndTime in Model.EndTimeList)
                                                    {
                                                    if (itemEndTime.Text == "17:00")
                                                    {
                                                    <option value="@itemEndTime.Value" selected>@itemEndTime.Text</option>
                                                    }
                                                    else
                                                    {
                                                    <option value="@itemEndTime.Value">@itemEndTime.Text</option>
                                                    }
                                                    }
                                                </select>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>

JQUERY

$('#bookingaction').click(function () {
            var LocumId = $('#LocumId').val();
            var JobPostingId = $('#JobPostingId').val();
            var BookingRate = $('#BookingRate').val();
            var BookingMessage = $('#BookingMessage').val();
            var bookingAction = $('#bookingaction').val();
            var bookingStatusId = $('#BookingStatusId').val();
            var bookingCreatedBy = $('#BookingCreatedBy').val();
            var availableDates = new Array();


            var stulist = new Array();
            $("#dt_basicA tr:not(:first)").each(function () {
                var tds = $(this).find("td");
                //you could use the Find method to find the texbox or the dropdownlist and get the value.
                var SStudent = { StartDate: $(tds[1]).html(), NewStartTime: $(tds[5]).find("#NewStartTime").val() }
                stulist.push(SStudent);
            });

            $.ajax({
                type: "POST",
                traditional: true,
                url: '@Url.Action("MyBookings", "Bookings")',
                data: { "bookingaction": bookingAction, "locumId": LocumId, "bookingStatusId": bookingStatusId, "bookingCreatedBy": bookingCreatedBy, "jobPostingId": JobPostingId, "bookingRate": BookingRate, "bookingMessage": BookingMessage, "availableDates": JSON.stringify(stulist) },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert("record(s) inserted.");
                }
            });
            //alert(bookingAction);

        });

控制器

        [HttpPost]
        public ActionResult MyBookings(string bookingaction, int locumId, int bookingStatusId, string bookingCreatedBy, int jobPostingId, float bookingRate, string bookingMessage, List<MyBookingEventsViewModel> availableDates)
        {
            ....Code comes here
        }

查看模型

public class MyBookingEventsViewModel
    {
        public string StartDate { get; set; }

        public string StartTime { get; set; }

        public string EndTime { get; set; }

        public string NewStartTime { get; set; }

        public string NewEndTime { get; set; }

        public string UserId { get; set; }

        public int LocumId { get; set; }

        public string DayOfTheWeek { get; set; }
    }

感谢您的帮助!

0 个答案:

没有答案