Question

I have an action that should prompt the user if the current object has children. The user should be able to elect to update the children as well, or not. How to I handle the user responds to this prompt?

<tr>
            <td><button type="submit" class="button">Save</button></td>
            @if (Model.OrganizationHasChildren)
            {
                <td>@Html.ActionLink("Save", "AleMasterData", null, new {@class = "button",onclick = "return confirm('Do you want to apply the ALE Indicator change to the sub-clients also?');" })</td>
            }
            else
            {
                <td>@Html.ActionLink("Save", "AleMasterData", null, new {@class = "button"})</td>
            }
        </tr>
Was it helpful?

Solution

Easiest option, build a ViewModel with an extra boolean, that would be represented in the markup with an:

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

And update it with a bit of Jquery

    function submitForm() {
        var l_choice = confirm('Do you want to apply the ALE Indicator change to the sub-clients also?');
        $('#ChoiceBool').val(l_choice);              
        $('#SaveForm').submit();
    }

With of course

        @if (Model.OrganizationHasChildren)
        {
            <td>@Html.ActionLink("Save", "AleMasterData", null, new {@class = "button", onclick = "submitForm();" })</td>
        }
        else
        {
            <td>@Html.ActionLink("Save", "AleMasterData", null, new {@class = "button"})</td>
        }

And then you should be able to retrieve it in the action server side

Or you could update the URL in the same JS method with the choice value, and retrieve it server side with a prototype like so

 public ActionResult Save(bool choice, ViewModel viewModel)

But that's more complicated for the same result, will research a bit more if you want to try the second option though

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top