在asp .net MVC中提交表单,各种错误

时间:2018-03-06 14:33:58

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

    [HttpGet]
    public ActionResult TrafficDate()
    {
        CalendarModel model = new CalendarModel();
        model.SelectedDate = DateTime.Today;
        model.EditMode = false;
        //model.TrafficDates = TrafficData.GeTrafficDatesPerMonth(model.SelectedDate);

        return View(model);
    }

    // GET: TrafficDate
    [HttpGet]
    public ActionResult TrafficDate(DateTime date, bool editMode = false)
    {
        CalendarModel model = new CalendarModel();
        model.SelectedDate = date;
        model.EditMode = editMode;
        //model.TrafficDates = TrafficData.GeTrafficDatesPerMonth(model.SelectedDate);

        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult TrafficDate(CalendarModel model)
    {

最上面的ActionResult被添加了,因为它一直在抱怨没有无参数的方法。现在它抱怨在上面的标签中有两个HttpGet的方法之间存在歧义,当我将无参数切换为HttpPost时,我在两个HttpPost之间得到相同的东西。

我希望我的提交按钮转到三者的底部方法。但它似乎想要一个无参数的方法,这是否意味着当我点击提交时模型是空的?

这是.cshtml文件的一部分

    @model Intern.Models.CalendarModel
@{
    ViewBag.Title = "Trafikkalender";
    //Layout = "_Layout";
}

<script>

</script>


<form method="post" id="form">
    <div id="hiddenFields" hidden="hidden">
        @Html.AntiForgeryToken()
        @Html.HiddenFor(m => m.SelectedDate)
        @Html.HiddenFor(m => m.EditMode, new { id = "editMode" })
        @Html.HiddenFor(m => m.SubmitMode, new { id = "submitMode" })
        @Html.HiddenFor(m => m.RegisterPersonOnThisResourceId, new { id = "registerForResourceId" })
        @Html.HiddenFor(m => m.PersonToRegister, new { id = "selectedPerson" })
        @Html.HiddenFor(m => m.TimeFrom, new { id = "hiddenTimeFrom" })
        @Html.HiddenFor(m => m.TimeTo, new { id = "hiddenTimeTo" })
        @Html.HiddenFor(m => m.Message, new { id = "hiddenMessage" })
        @Html.HiddenFor(m => m.NewTrafficType, new { id = "hiddenNewTrafficType" })
        @Html.HiddenFor(m => m.NewTrafficText, new { id = "hiddenNewTrafficText" })
        @Html.HiddenFor(m => m.NewTrafficNumTrains, new { id = "hiddenSelectedNewTrafficNumTrains" })
        @Html.HiddenFor(m => m.NewTrafficNumConductors, new { id = "hiddenSelectedNewTrafficNumConductors" })
        @Html.HiddenFor(m => m.CopyTraffic, new { id = "hiddenCopyTraffic" })
        @Html.HiddenFor(m => m.NewTrafficDate, new { id = "hiddenNewTrafficDate" })
    </div>
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="panel-heading">
                <h1 id="header" class="page-header">Trafik - @((DayNames)((int)Model.SelectedDate.DayOfWeek)) den @Model.SelectedDate.Day @((MonthNames)Model.SelectedDate.Month) @Model.SelectedDate.Year</h1>

            </div>
            <div class="row">
                <div class="col-md-8"></div>
                <div class="col-md-4">
                    @Html.ActionLink("Föregående dag", "TrafficDate", new { date = Model.SelectedDate.AddDays(-1), editMode = Model.EditMode }, new { @class = "btn btn-primary" })
                    @Html.ActionLink("Nästa dag", "TrafficDate", new { date = Model.SelectedDate.AddDays(1), editMode = Model.EditMode }, new { @class = "btn btn-primary" })
                </div>
            </div>

        </div>
    </div>
    <div class="panel">

        @if (UserData.isInRole(Convert.ToInt32(User.Identity.Name), "Administratör"))
        {
            <div class="row">
                <div class="col-lg-12">
                    @Html.ActionLink("Redigera dag(beta)", "ToggleEditMode", "TrafficDate", new { date = Model.TrafficDate.Date, editMode = Model.EditMode }, new { @class = "btn btn-primary" })
                    <input onclick="javascript: EditDay();" type="button" value="Redigera dag" id="editDayButton" name="editDayButton" class="btn btn-primary" />
                    <input onclick="javascript: CancelEditDay();" type="button" value="Avsluta redigering" id="cancelEditDayButton" name="cancelEditDayButton" class="btn btn-primary hidden" />
                    @Html.ActionLink(" ", "PrintDay", new { date = Model.SelectedDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) }, new { @class = "glyphicon glyphicon-print" })
                </div>
            </div>
        }

        @if (Model.EditMode)
        {
            <div class="panel panel-default">
                <div class="panel-body">
                    @for (int ti = 0; ti < Model.TrafficDate.Traffics.Count; ti++)
                    {
                        <div class="panel panel-default form-group">
                            <div class="panel-heading">
                                @(Html.DropDownListFor(m => m.TrafficDate.Traffics[ti].TrafficType, TrafficData.GetTrafficTypes(Model.TrafficDate.Traffics[ti].TrafficType.ToString()), new { @class = "ddlTrafficType", @id = "ddlTrafficType_" + ti }))
                                @Html.CheckBoxFor(m => m.TrafficDate.Traffics[ti].Published, new { @class = "cbTrafficPublished", @id = "cbPublished_" + ti }) Publicerad
                                [<input type="button" value="Radera" class="deleteTraffic btn btn-link NoBorder NoBackGround" data-id="@Model.TrafficDate.Traffics[ti].Id" data-url="@Url.Action("DeleteTraffic")" data-confirm="Är du säker på att du vill radera denna trafik?" />]
                                <input type="button" class="saveTraffic btn btn-primary btn-sm float-right" value="Spara (@Model.TrafficDate.Traffics[ti].TrafficType.ToString())" data-url="@Url.Action("SaveTraffic")" data-id="@Model.TrafficDate.Traffics[ti].Id" data-confirm="Är du säker på att du vill skriva över de nuvarande inställningarna på denna trafik?" />
                                <input type="submit" class="btn btn-primary" value="Save traffic (beta)" />

                            </div>

提交按钮位于代码剪切的倒数第二行。 是我的.cshtml文件中的问题?或者我以某种方式定义了控制器方法错误?一开始的隐藏字段是我为处理事物的保存所做的一个javascript,但我想摆脱javascript并使其成为正确的方法。

另外。代码中的表单组类,我在一个例子中看到,那重要吗?在每个div中我都需要它吗?或者它是否足以将它包含在我想提交的事物中涉及的整个html块的div中?

1 个答案:

答案 0 :(得分:1)

正如所讨论的那样

[HttpGet]
public ActionResult TrafficDate(DateTime? date, bool? editMode = false)
{
     return View();
}

然后删除无参数的TrafficDate操作,只留下您的帖子。

我在当地尝试过,似乎没问题 感谢