C# 模型不从动态添加的内容向控制器返回值?

C# 模型不从动态添加的内容向控制器返回值?,c#,jquery,asp.net-core,C#,Jquery,Asp.net Core,我有可以使用Ajax从代码隐藏动态添加的计划时间。用户可以单击add按钮,jQueryAjax调用应该添加另一组字段来添加时间表时间。出于某种原因,当我单击“保存”时,我的模型没有向控制器返回任何值 我尝试使用另一个模型,该模型还包括日期、开始时间和结束时间,但也没有返回值。字段中的值似乎不是从表单中发布的 HTML表单 <form asp-action="SetSchedule" asp-controller="Admin" method="post" id="addEventForm"

我有可以使用Ajax从代码隐藏动态添加的计划时间。用户可以单击add按钮,jQueryAjax调用应该添加另一组字段来添加时间表时间。出于某种原因,当我单击“保存”时,我的模型没有向控制器返回任何值

我尝试使用另一个模型,该模型还包括日期、开始时间和结束时间,但也没有返回值。字段中的值似乎不是从表单中发布的

HTML表单

<form asp-action="SetSchedule" asp-controller="Admin" method="post" id="addEventForm">
                <table id="scheduleTable">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Start Time</th>
                            <th>End Time</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>

                <br />
                <button id="addScheduleButton" type="button">Add Schedule Time</button>
                <button id="savePendingButton" type="submit" style="margin-left:1em" hidden>Save</button>
            </form>
型号:

public class ScheduleViewModel
    {
        public List<Schedule> ScheduleItems { get; set; } = new List<Schedule>();

        //public InterviewInfoApplicationViewModel InterviewInfo { get; set; } = new InterviewInfoApplicationViewModel();
    }

    public class Schedule
    {
        public int Id { get; set; }

        public DateTime DateAvailable { get; set; }

        [Required]
        public DateTime StartTime { get; set; }

        [Required]
        public DateTime EndTime { get; set; }

        public bool IsSelected { get; internal set; }
    }

模型应将DateAvailable、StartTime和EndTime返回给控制器。

您的操作将接收模型类型为
ScheduleViewModel
,其属性为
List
,而您的视图
asp for=“DateAvailable”
既不绑定到
ScheduleViewModel
也不绑定到
List

请参阅下面的演示:

1.AddScheduleItem操作以将
索引
返回到局部视图

public IActionResult AddScheduleItem(string index)
    {
        var myViewData = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()) 
                         { 
                            { "index", index }
                         };
        PartialViewResult result = new PartialViewResult()
        {
            ViewName = "YourPartialViewName",
            ViewData = myViewData,
        };

        return result;

    }
2.YourPartailView:

@model ScheduleViewModel

@{
    string rowClass = "pendingScheduleRow";
    int index = Int32.Parse(@ViewData["index"].ToString());
}

<tr class="@rowClass" style="margin-top:1em">
    <td>
        @Html.ValidationMessageFor(x => x.ScheduleItems[index].DateAvailable)
        <input asp-for="ScheduleItems[index].DateAvailable" class="datepickerInput" />
    </td>
    <td>
        @Html.ValidationMessageFor(x => x.ScheduleItems[index].StartTime)
        <input asp-for="ScheduleItems[index].StartTime" class="timepickerInput" />
    </td>
    <td>
        @Html.ValidationMessageFor(x => x.ScheduleItems[index].EndTime)
        <input asp-for="ScheduleItems[index].EndTime" class="timepickerInput" />
    </td>
    <td>
        <a class="tooltip deletePendingSchedule" title="Remove Schedule Time" style="cursor:pointer">
            <span class="wdn-icon-cancel" aria-hidden="true"></span><span class="wdn-text-hidden">cancel icon</span>
        </a>
    </td>
</tr>
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
@model ScheduleViewModel
@{
字符串rowClass=“pendingScheduleRow”;
int index=Int32.Parse(@ViewData[“index”].ToString());
}
@Html.ValidationMessageFor(x=>x.ScheduleItems[index].DateAvailable)
@Html.ValidationMessageFor(x=>x.ScheduleItems[index].StartTime)
@Html.ValidationMessageFor(x=>x.ScheduleItems[index].EndTime)
取消图标
@{wait Html.RenderPartialAsync(“_validationScript”);}
    require(['jquery', 'jqueryui'], function ($, jqueryui) {

        $(function () {
  $('body').on('focus', ".datepickerInput", function () {
                    $(this).datepicker({
                        controlType: "select",

                    });
                });

                $('body').on('focus', ".timepickerInput", function () {
                    $(this).timepicker({
                        timeFormat: "hh:mm tt",
                        interval: 5,
                        dropdown: true,
                        scrollbar: true
                    });
                });

                $("#addScheduleButton").click(function () {
                    var nextIndex = $(".pendingScheduleRow").length;

                    $.ajax({
                        url: "/Admin/AddScheduleItem",
                        type: 'POST',
                        data: {
                            index: nextIndex
                        },
                        success: function (results) {
                            $("table#scheduleTable tbody").append(results);
                            $("#savePendingButton").show();
                        }
                    });
                });

                $("#scheduleTable").on("click", ".deletePendingSchedule", function () {

                    var closestRow = $(this).closest("tr");

                    $(closestRow).hide();

                    var visibleRows = $(".pendingScheduleRow:visible").length;

                    if (visibleRows === 0) {

                        $("#savePendingButton").hide();
                    }

                });

        });

    });
public IActionResult AddScheduleItem(string index)
    {
        var myViewData = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()) 
                         { 
                            { "index", index }
                         };
        PartialViewResult result = new PartialViewResult()
        {
            ViewName = "YourPartialViewName",
            ViewData = myViewData,
        };

        return result;

    }
@model ScheduleViewModel

@{
    string rowClass = "pendingScheduleRow";
    int index = Int32.Parse(@ViewData["index"].ToString());
}

<tr class="@rowClass" style="margin-top:1em">
    <td>
        @Html.ValidationMessageFor(x => x.ScheduleItems[index].DateAvailable)
        <input asp-for="ScheduleItems[index].DateAvailable" class="datepickerInput" />
    </td>
    <td>
        @Html.ValidationMessageFor(x => x.ScheduleItems[index].StartTime)
        <input asp-for="ScheduleItems[index].StartTime" class="timepickerInput" />
    </td>
    <td>
        @Html.ValidationMessageFor(x => x.ScheduleItems[index].EndTime)
        <input asp-for="ScheduleItems[index].EndTime" class="timepickerInput" />
    </td>
    <td>
        <a class="tooltip deletePendingSchedule" title="Remove Schedule Time" style="cursor:pointer">
            <span class="wdn-icon-cancel" aria-hidden="true"></span><span class="wdn-text-hidden">cancel icon</span>
        </a>
    </td>
</tr>
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}