c# - HTTP Post 使用提交按钮将信息从 HTML.BeginForm 发布到控制器
问题描述
我尝试通过提交按钮将信息从视图提交到控制器,但出现 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" />
}
解决方案
您应该将您的操作更新为ScheduleInfo
而不是schedulerIndex
,因此Html.BeginForm
将是
@using (Html.BeginForm("ScheduleInfo", "Scheduler", FormMethod.Post))
推荐阅读
- php - PHP 使用 SSL 证书连接到 restAPI
- php - 计数从 PHP 中的大型 mysql 查询返回的 Int 值的出现次数
- vscode-extensions - 为 vscode 扩展实现 DocumentHighlightProvider 有什么意义
- android-jetpack-compose - 在水平寻呼机中保持列表状态 - Jetpack Compose
- javascript - 如何检测window.open
- java - 如何使用 Swagger Core (springdoc-openapi) 生成 OpenAPI 任意类型?
- python - 在 Python 中复制 R 解决方案以计算每月 CRR
- javascript - 生成图表时出错:Google 地球引擎中超出了用户内存限制
- sas - 我可以在 sas 的数据步骤中使用宏吗?
- python - 如何使用以前使用笔记本保存的自定义类加载 joblib 文件?