为什么单击actionResult控件转到Get part而不发布部分action方法?

问题描述 投票:0回答:2

我在控制器中有这个方法。

[HttpPost]
        public ActionResult ChangeStatus(int id)
        {
            try
            {
                using (UnitOfWork uwork = new UnitOfWork())
                {
                    InspectionReportDAL = new InspectionReportDAL();
                    User user = (User)Session["User"];

                    InspectionReport InspectionReport = uwork.InspectionReportRepository.GetByID(id);

                    if (id == 1) //Reviewed
                    {
                        InspectionReport.CheckedBy = user.UserID;
                        InspectionReport.Status = (byte) id;
                    }
                    else if (id == 2) //Approved
                    {
                        InspectionReport.ApprovedBy = user.UserID;
                        InspectionReport.Status = (byte)id;
                    }
                    else if (id == 3) //Issued
                    {
                        InspectionReport.IssuedBy = user.UserID;
                        InspectionReport.Status = (byte)id;
                    }

                    uwork.Save();

                    return RedirectToAction("Index", "InspectionReport");
                }
            }
            catch (Exception ex)
            {

                return View();
            }

        }

我使用动作链接调用它,从编辑视图中使用同一控制器中的操作方法

@Html.ActionLink("Review", "ChangeStatus", new { id = 1 }, new { @class = "btn btn-success" })

控制器名称是InspectionResult。

当我单击ActionLink时,它会转到ChangeStatus方法,但是Get的一个不是post的一个。

 [HttpGet]
        public ActionResult ChangeStatus()

        { return View(); }

Route.Config

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
            );
        }

此外,ActionLink位于

@using (Html.BeginForm())

更新:完整代码

@model VAILCertificates.DAL.Entities.InspectionReport

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>InspectionReport</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.InspectionReportID)

        <div class="form-group">
            @Html.LabelFor(model => model.VelosiProjectNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.VelosiProjectNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.VelosiProjectNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.VelosiReportNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.VelosiReportNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.VelosiReportNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Reference, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Reference, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Reference, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PoNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.PoNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PoNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InspectionDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.InspectionDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InspectionDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IssueDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.IssueDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.IssueDate, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.InspectionPhase, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.DropDownListFor(model => model.InspectionPhase, new List<SelectListItem>
                   {
                     new SelectListItem { Text = "Before", Value = "0"},
                     new SelectListItem { Text = "During", Value = "1"},
                     new SelectListItem { Text = "Final", Value = "2"}
                  }, "-Select-",
                  new
                  {
                      @Style = "Width:500px;height:40px;",
                      @class = "form-control input-lg"
                  })
                @Html.ValidationMessageFor(model => model.InspectionPhase, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InServiceInspection, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.InServiceInspection)
                    @Html.ValidationMessageFor(model => model.InServiceInspection, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewInduction, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.NewInduction)
                    @Html.ValidationMessageFor(model => model.NewInduction, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HydrostaticTest, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.HydrostaticTest)
                    @Html.ValidationMessageFor(model => model.HydrostaticTest, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DimensionalCheck, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.DimensionalCheck)
                    @Html.ValidationMessageFor(model => model.DimensionalCheck, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ThicknessCheck, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.ThicknessCheck)
                    @Html.ValidationMessageFor(model => model.ThicknessCheck, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Patrom, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Patrom)
                    @Html.ValidationMessageFor(model => model.Patrom, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Gvs, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Gvs)
                    @Html.ValidationMessageFor(model => model.Gvs, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FinalOgraInspection, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.FinalOgraInspection)
                    @Html.ValidationMessageFor(model => model.FinalOgraInspection, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.OmcClientRequirement, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.OmcClientRequirement)
                    @Html.ValidationMessageFor(model => model.OmcClientRequirement, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TankLorryRegistrationNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.TankLorryRegistrationNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TankLorryRegistrationNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TruckTractorManufacturerName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.TruckTractorManufacturerName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TruckTractorManufacturerName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ClientName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.ClientName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ClientName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Capacity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Capacity, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Capacity, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Omc, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Omc, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Omc, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EngineNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.EngineNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EngineNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TankLorryDimension, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.TankLorryDimension, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TankLorryDimension, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ChassisNo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.ChassisNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ChassisNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InspectionPlace, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.InspectionPlace, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InspectionPlace, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TankLorryEnginePower, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.TankLorryEnginePower, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TankLorryEnginePower, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CarriageName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.CarriageName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CarriageName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Brakes, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Brakes, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Brakes, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsSatisfactory, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                <div class="checkbox">
                    @Html.EditorFor(model => model.IsSatisfactory)
                    @Html.ValidationMessageFor(model => model.IsSatisfactory, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Remarks, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Remarks, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Remarks, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Rev, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Rev, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Rev, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
        </div>

        @*<div class="form-group">
                @Html.LabelFor(model => model.PeparedBy, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-3">
                    @Html.EditorFor(model => model.PeparedBy, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.PeparedBy, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CheckedBy, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-3">
                    @Html.EditorFor(model => model.CheckedBy, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.CheckedBy, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ApprovedBy, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-3">
                    @Html.EditorFor(model => model.ApprovedBy, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ApprovedBy, "", new { @class = "text-danger" })
                </div>
            </div>*@

        <div class="form-group">
            @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @{
    if (Model.Status == 0)
    {
        @Html.Label("Prepared", htmlAttributes: new { @class = "label label-primary" })
    }
    else if (Model.Status == 1)
    {
        @Html.Label("Reviewed", htmlAttributes: new { @class = "label label-info" });
    }
    else if (Model.Status == 2)
    {
        @Html.Label("Approved", htmlAttributes: new { @class = "label label-success" });
    }
    else if (Model.Status == 3)
    {
        @Html.Label("Issued", htmlAttributes: new { @class = "label label-default" });
    }
                }

            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-12">

                @{

    VAILCertificates.DAL.Entities.User user = (VAILCertificates.DAL.Entities.User)Session["User"];

    if (user != null)
    {
        if ((user.UserGroupID == 3 || user.UserGroupID == 1) && Model.Status < 1)  //Preparator
        {
            <input type="submit" value="Save" class="btn btn-success" />
        }
        else if (user.UserGroupID == 4 && Model.Status == 0)  //Reviewer
        {
            @Html.ActionLink("Review", "ChangeStatus", new { id = 1 }, new { @class = "btn btn-success" })

           <input type="submit" value="Save" class="btn btn-success" />
        }
        else if (user.UserGroupID == 1003 && Model.Status == 1)  //Approver
        {
            @Html.ActionLink("Approve", "ChangeStatus", new { id = 2 }, new { @class = "btn btn-success" })
        }
        else if (user.UserGroupID == 1004 && Model.Status == 2)  //Issuer
        {
            @Html.ActionLink("Issue", "ChangeStatus", new { id = 3 }, new { @class = "btn btn-success" })
        }
    }
                }


            </div>
        </div>
    </div>
}


<script>
    $(function () {
        $("#IssueDate").datepicker();
        $("#InspectionDate").datepicker();
    });


</script>
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
c# asp.net-mvc post model-view-controller actionresult
2个回答
1
投票

那么你可以尝试的是注释@ Html.BeginForm()并且它是两个大括号{}然后尝试用@Html.ActionLink("InspectionResult", "ChangeStatus", new { id = 1 }, new { @class = "btn btn-success" })命中你的更改状态方法我之前有一个类似的问题,之前BeginForm试图在我的get方法之后点击post方法并重新加载表单。


0
投票

好的,谢谢你的帮助。

但我明白了。

这很有效。

 <button type="submit" id="button1" name="button1" formaction='@Url.Action("ChangeStatus", "InspectionReport", new { id = 1, recordID = Model.InspectionReportID })'>Save</button>
© www.soinside.com 2019 - 2024. All rights reserved.