locked
How to validate with only button in same form has 2 buttons? RRS feed

  • Question

  • User-1714009740 posted

    I have a form with 2 buttons with 2 values, Confirm and Reject, they're in same action.

    When Reject button is pressed without any comment in Textarea, form will inform 'Please input this field'

    When Confirm button is pressed, it doesn't need to validate, just go to the action.

    This is my view

    @if (Model.signingFlowViewModel.signingFlows.Count(s => s.SigningUser == User.Identity.Name && s.Status == "1") > 0)
    {
       using (Html.BeginForm("OvertimeSigningAsync", "Signing", FormMethod.Post,
          new { onsubmit = "return confirm('Do you really want to submit the form?');" }))
       {
    <div class="row" style="margin-top:10px;">
        <div class="col-sm-3">
            <div class="form-group">
                <input type="submit"
                       name="Sign" value="Confirm"
                       class="form-control btn btn-primary"
                       style="margin-bottom: 5px" />
                <br>
                <input type="submit"
                       name="Sign" value="Reject"
                       class="form-control btn btn-danger"/>
            </div>
        </div>
        <div class="col-sm-9">
            <div class="form-group">
                @Html.TextBoxFor(m => m.DocNo, new { @class = "hidden" })
                @Html.TextBoxFor(m => m.FlowNo, new { @class = "hidden" })
                @Html.TextBoxFor(m => m.Plant, new { @class = "hidden" })
                @Html.TextAreaFor(m => m.Comment, new { @class = "form-control", @required  = "required" })
                @Html.TextBoxFor(m => m.NewDocNo, new { @class = "hidden" })
            </div>
        </div>
    </div>
       }
    }

    And this is action 

    public async Task<ActionResult> OvertimeSigningAsync([Bind(Include = "Plant,FlowNo,DocNo,Comment,NewDocNo")] 
    OvertimeSigningViewModel overtimeSigningViewModel, string Sign)
    {
        bool result = false;
        if (ModelState.IsValid && !String.IsNullOrWhiteSpace(Sign))
        {
            result = eISRepository.EditPurchaseRequest(
                        overtimeSigningViewModel.Plant,
                        overtimeSigningViewModel.FlowNo,
                        overtimeSigningViewModel.DocNo,
                        User.Identity.Name, Sign,
                        overtimeSigningViewModel.Comment, 
                        overtimeSigningViewModel.NewDocNo);
        }
    }

    How can I handle it?

    I'm a newbie in MVC so please help

    Edit: I have found the solution, I add formnovalidate attribute to Confirm button, it'll OK

    Tuesday, June 25, 2019 3:51 AM

Answers

  • User-1714009740 posted

    Just add formnovalidate attribute to Confirm button, it'll OK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 26, 2019 9:29 AM

All replies

  • User1520731567 posted

    Hi VuHue,

    I'm glad that you have solved the issue by yourself.

    I suggest you could post the details answer as reply and mark it as answer.

    This will help other people who faces the same issue.

    Thank you very much.

    Best Regards.

    Yuki Tao

    Wednesday, June 26, 2019 8:54 AM
  • User-1714009740 posted

    Just add formnovalidate attribute to Confirm button, it'll OK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 26, 2019 9:29 AM