-1

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>
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
  • 3
    DataAccess layer in the view? :-/ – Darren Sep 09 '13 at 15:13
  • 1
    `confirm` only has `true` and `false` return values. There's no way to tell between the user wanting to say "no" to your question, and wanting to cancel altogether. See http://stackoverflow.com/q/3110825/781792 if this is a problem for you. – Tim S. Sep 09 '13 at 15:15
  • @DarrenDavies you're right I'll fix that – Antarr Byrd Sep 09 '13 at 15:15
  • 2
    As a general idea: after they click the button, open the confirm dialog. Once they're done, submit the request with their boolean decision as a parameter. You might be able to do this with an `onclick` event with jQuery, or by making the link only trigger your JS code, and the JS code makes the call via AJAX or other approach. – Tim S. Sep 09 '13 at 15:20

1 Answers1

1

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

Simon Rapilly
  • 393
  • 1
  • 13