Mobiles Malta
Mobiles Malta

Reputation: 25

Submit button not working! MVC

@model CommonLayer.ORDER
@{
ViewBag.Title = "Update";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h4>Update Order Status</h4>
<br />

@if (Model.OrderStatus != "Shipped")
{
using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.UserId)

        <div class="form-group">
            @Html.LabelFor(model => model.OrderId, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select class="form-control" name="OrderStatus">
                    <option value="Pending" id="pending">Pending</option>
                    <option value="Shipped" id="shipped">Shipped</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            </div>
        </div>

        <form action="/order/update/@Model.OrderId" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Update" class="btn btn-primary" />
                </div>
            </div>
        </form>
    </div>
}
}
else
{
<h4>Order Has Been Shipped!</h4>
}

My submit button isn't working regardless of the correct syntax being used. When I press the button nothing happens, as if I haven't clicked it, no redirecting whatsoever.

Was working until yesterday. Did not change any code whatsoever regarding the form or corresponding controller.

This is the controller corresponding to the form.

[HttpGet]
    [Authorize(Roles = "ADM")]
    public ActionResult Update(Guid Id)
    {
        BusinessLayer.Orders order = new BusinessLayer.Orders();
        return View(order.GetOrder(Id));
    }

    [HttpPost]
    [Authorize(Roles = "ADM")]
    public ActionResult Update(CommonLayer.ORDER order, Guid id)
    {
        BusinessLayer.Orders blorder = new BusinessLayer.Orders();
        blorder.UpdateOrder(order);
        return RedirectToAction("UpdateDetails", new {id=id});
    }

Upvotes: 0

Views: 7090

Answers (1)

Mir Gulam Sarwar
Mir Gulam Sarwar

Reputation: 2648

Use below code, let us know if doesn't work

Added below line to pass the orderid

@Html.HiddenFor(model => model.OrderId)

Updated below line to call action of a controller

Html.BeginForm("update","order",FormMethod.Post)

Finally removed the unnecessary form tag

Removed antiforgerytoken from view as you are not checking in you action method. If you want to add that then you need to add [ValidateAntiForgeryToken] after [httppost] of Update Action method

@model CommonLayer.ORDER
    @{
    ViewBag.Title = "Update";
    Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h4>Update Order Status</h4>
    <br />

    @if (Model.OrderStatus != "Shipped")
    {
    using (Html.BeginForm("Update","Order",FormMethod.Post))
    {
        <div class="form-horizontal">
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.UserId)
            @Html.HiddenFor(model => model.OrderId)
            <div class="form-group">
                @Html.LabelFor(model => model.OrderId, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderStatus, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select class="form-control" name="OrderStatus">
                        <option value="Pending" id="pending">Pending</option>
                        <option value="Shipped" id="shipped">Shipped</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.OrderNumber, htmlAttributes: new { @style = "font-size:medium", @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OrderNumber, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                </div>
            </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Update" class="btn btn-primary" />
                    </div>
                </div>
        </div>
    }
    }
    else
    {
    <h4>Order Has Been Shipped!</h4>
    }

Upvotes: 4

Related Questions