HTTP Post通过提交按钮

时间:2018-04-30 14:05:31

标签: c# asp.net-mvc view controller

我尝试通过提交按钮将视图中的信息提交给控制器,但我收到404错误。我假设必须在这里使用HTTPPost方法,因此为什么我将它包含在标题中,但我无法让它将我的信息传递给控制器​​。我已经传递了ScheduleInfo ActionResult中的字段,使用了HTTPPost并将FormMethod.Post添加到视图中的Html.BeginForm。

这是我正在使用的控制器。

public class SchedulerController : Controller
{
    // GET: Scheduler
    [HttpGet]
    public ActionResult SchedulerIndex()
    {
        List<Values> lst = new List<Values>();
        List<Values> lst2 = new List<Values>();
        List<Values> lst3 = new List<Values>();


        foreach (Values.Months month in Enum.GetValues(typeof(Values.Months)))
        {
            lst.Add(new Values { Month = month, IsChecked = false });
        }

        foreach (Values.DaysOfTheWeek day in Enum.GetValues(typeof(Values.DaysOfTheWeek)))
        {
            lst2.Add(new Values { DaysOfWeek = day, IsChecked = false });
        }

        var DaysOfTheMonth = Enumerable.Range(1, 31);

        foreach (var days in DaysOfTheMonth)
        {
            lst3.Add(new Values { DayOfMonth = days, IsChecked = false });
        }


        var val = new Values();
        val.Jobs = new List<SelectListItem>();
        val.Jobs.Add(new SelectListItem() { Text = "Email", Value = "1", Selected = false });
        val.Jobs.Add(new SelectListItem() { Text = "Backup", Value = "2", Selected = false });
        val.Jobs.Add(new SelectListItem() { Text = "Start Application", Value = "3", Selected = false });
        val.Jobs.Add(new SelectListItem() { Text = "Job4", Value = "4", Selected = false });
        val.Jobs.Add(new SelectListItem() { Text = "Job5", Value = "5", Selected = false });


        ViewBag.lst = lst;
        ViewBag.lst2 = lst2;
        ViewBag.lst3 = lst3;

        return View(val);
    }

    [HttpPost]
    public ActionResult ScheduleInfo(string Job, int Second, int Minute, int Hour, int Month, int DaysOfWeek, int DayOfMonth)
    {
        var val = new Values();

        val.Second = Second;
        val.Minute = Minute;
        val.Hour = Hour;
        val.Month = Month;
        val.DaysOfWeek = DaysOfWeek;
        val.DayOfMonth = DayOfMonth;

        return View();
    }
}

这是我正在使用的ViewModel:

public class Values
{
    public enum Months
    {
        Jan = 0,
        Feb = 1,
        Mar = 2,
        Apr = 3,
        May = 4,
        Jun = 5,
        Jul = 6,
        Aug = 7,
        Sep = 8,
        Oct = 9,
        Nov = 10,
        Dec = 11
    }

    public enum DaysOfTheWeek
    {
        Mon = 0,
        Tue = 1,
        Wed = 2,
        Thu = 3,
        Fri = 4,
        Sat = 5,
        Sun = 6
    }

    public int DayOfMonth { get;  set; }

    public object Month { get; set; }

    public object DaysOfWeek { get; set; }

    public bool IsChecked { get; set; }

    public int Second { get; set; }
    public int Minute { get; set; }
    public int Hour { get; set; }

    public int Job { get; set; }
    public List<SelectListItem> Jobs { get; set; }
}

以下是我从以下网站获取信息的视图:

 @using (Html.BeginForm("schedulerIndex", "Scheduler", FormMethod.Post))
{
    <center>
        <div style="text-align: center">
            <div class="form-group">
                        @Html.DropDownListFor(m => m.Job, Model.Jobs)
            </div>
        </div>
    </center>

    <center>
        <div class="form-group" style="display:inline-block; padding-right:100px">
            <div class=" col-md-2">
                @Html.LabelFor(model => model.Second, "Second")
                @Html.TextBoxFor(model => model.Second)
            </div>
        </div>
        <div class="form-group" style="display:inline-block; padding-right:100px">
            <div class=" col-md-2">
                @Html.LabelFor(model => model.Minute, "Minute")
                @Html.TextBoxFor(model => model.Minute)
            </div>
        </div>
        <div class="form-group" style="display:inline-block; padding-right:100px">
            <div class=" col-md-2">
                @Html.LabelFor(model => model.Hour, "Hour")
                @Html.TextBoxFor(model => model.Hour)
            </div>
        </div>
    </center>
    <br />
    <br />

    <center>
        <div class="col-1">
            <ul class="list-group">
                <li class="list-group-item-heading list-group-item active">
                    <h4 class="list-group-item-text">Select the month(s) the task should be set at</h4>
                </li>

                @foreach (var item in ViewBag.lst)
                {

                    <li class="list-group-item" style="display:inline-block">
                        <div class="checkbox-inline">
                            <input type="checkbox" id="Check_@item.Month" checked="@item.IsChecked" />
                            <label for="Check @item.Month">@item.Month</label>
                        </div>
                    </li>
                }
            </ul>
        </div>
    </center>


    <center>
        <div class="col-2">
            <ul class="list-group">
                <li class="list-group-item-heading list-group-item active">
                    <h4 class="list-group-item-text">Select the day(s) of the week the task should be set at</h4>
                </li>

                @foreach (var item2 in ViewBag.lst2)
                {

                    <li class="list-group-item" style="display:inline-block">
                        <div class="checkbox-inline">
                            <input type="checkbox" id="Check_@item2.DaysOfWeek" checked="@item2.IsChecked" />
                            <label for="Check @item2.DaysOfWeek">@item2.DaysOfWeek</label>
                        </div>
                    </li>
                }
            </ul>
        </div>
    </center>


    <center>
        <div class="col-3">
            <ul class="list-group">
                <li class="list-group-item-heading list-group-item active">
                    <h4 class="list-group-item-text">Select the day(s) of the month the task should be set at</h4>
                </li>

                @foreach (var item3 in ViewBag.lst3)
                {

                    <li class="list-group-item" style="display:inline-block">
                        <div class="checkbox-inline">
                            <input type="checkbox" id="Check_@item3.DayOfMonth" checked="@item3.IsChecked" />
                            <label for="Check @item3.DayOfMonth">@item3.DayOfMonth</label>
                        </div>
                    </li>
                }
            </ul>
        </div>
    </center>

    <input type="submit" value="Submit Data" id="btnSubmit" />

}

1 个答案:

答案 0 :(得分:0)

您应该将您的操作更新为ScheduleInfo而不是schedulerIndex,因此Html.BeginForm

@using (Html.BeginForm("ScheduleInfo", "Scheduler", FormMethod.Post))