0
votes

I try to submit the information from view to controller via submit button but i get a 404 error. I'm assuming a HTTPPost method has to be used here hence why I have included it in the title however, i can't get it to pass my information to the controller. I've passed the fields in the ScheduleInfo ActionResult, used the HTTPPost and added FormMethod.Post to the Html.BeginForm in the view.

Here is the controller I am using.

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();
    }
}

Here is the ViewModel I am using:

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; }
}

Here is the View I am getting the information from:

 @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="[email protected]" 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="[email protected]" 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="[email protected]" 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

1 Answers

0
votes

You should update your action to ScheduleInfo instead of schedulerIndex, so the Html.BeginForm will be

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