0

I have a dynamic button, that has an attribute of Stepid. What i am trying to do is capture that attribute when the button is clicked and pass the same attribute into my model and assign the StepId value as my button Id in the modal.

My button

 <button class="btn btn-warning moveRecipeStep" id="blah" data-bind="attr: {'data-id': StepId, data_recipe_name: $parent.RecipeName}" data-toggle="modal" data-target="#moveRecipeReason">@Html.LocalisedStringFor(model => model.MoveToStageText)</button>

and my modal

    <section id="hidden">
        <div class="modal fade" id="moveReason" tabindex="-1" role="dialog" arial-labelledby="moveReasonLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="moveReasonLabel">What is the reason for the Step move?</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body reasonDialog">
                        <form>
                            <div class="form-group">
                                @Html.LabelFor(model => model.ReasonText)
                                @Html.TextAreaFor(model => model.ReasonText, new { rows = 4, @class = "form-control", maxlength = "100", data_bind = "value: Reason" })
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button id="DoMove" type="button" class="btn btn-primary">@Html.LocalisedStringFor(model => model.SubmitText)</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="moveRecipeReason" tabindex="-1" role="dialog" arial-labelledby="moveReasonLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="moveReasonLabel">What is the reason for the Recipe move?</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body reasonDialog">
                        <form>
                            <div class="form-group">
                                @Html.LabelFor(model => model.ReasonText)
                                @Html.TextAreaFor(model => model.ReasonText, new { rows = 4, @class = "form-control", maxlength = "100", data_bind = "value: Reason" , id = "blah" })
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button id="DoMoveRecipe" type="button" class="btn btn-primary">@Html.LocalisedStringFor(model => model.SubmitText)</button>
                    </div>
                </div>
            </div>
        </div>
    </section>

And my javascript

                    var input = $(this);
                    var buttonId = input.attr("id");
                    var id = input.data("id");
                    var url = buttonId === 'MoveNext' ? '@Url.Action("MakeNext")' : '@Url.Action("MoveRecipePosition")';

                    $("#moveReason").modal("toggle");
                    if (buttonId === "MoveNext") {
                        $.ajax(url,
                            {
                                data: {
                                    "Id": id,
                                    "Reason": $("#moveReason .reasonDialog textarea").val(),
                                },
                                cache: false,
                                method: "POST",
                            }).done(function(returnData) {
                            if (returnData) {
                                if (returnData.BrokenRule !== null) {
                                    alert(returnData.BrokenRule.Message);
                                } else if (returnData.ProcessStep !== null) {
                                    var bFound = false;
                                    //Done like this because you can only move backwards. Originally the logic was fuller but, most things don't change now.
                                    //The extra logic just hides everything past the active one
                                    for (var pos = 0; pos < viewModel.Stages().length; pos++) {
                                        if (bFound) {
                                            viewModel.Stages()[pos].Id(-1);
                                            viewModel.Stages()[pos].IsNext(false);
                                        } else if (viewModel.Stages()[pos].Id() == returnData.ProcessStep.Id) {
                                            viewModel.Stages()[pos].IsNext(returnData.ProcessStep.IsNext);
                                            viewModel.Stages()[pos].BenchId(returnData.ProcessStep.BenchId);
                                            viewModel.Stages()[pos].BenchName(returnData.ProcessStep.BenchName);
                                            viewModel.Stages()[pos].IsTransacted(returnData.ProcessStep.IsTransacted);
                                            viewModel.Stages()[pos].RecipeName(returnData.ProcessStep.RecipeName);

                                            bFound = true;
                                        }
                                    }
                                }
                            }
                        }).fail(function(xhr) {
                            try {
                                console.log(xhr.statusText);
                                console.log(xhr.responseText);
                                alert(xhr.statusText + "\r\n" + xhr.responseText);
                            } catch (ex) {
                                console.log(ex);
                                alert(ex);
                            }
                        });
                    } else {
                        $.ajax(url,
                            {
                                data: {
                                    "SerialNumber": viewModel.SerialNumber(),
                                    "Message": $("#moveRecipeReason .reasonDialog textarea").val(),
                                    "StepId": a
                                },
                                cache: false,
                                method: "POST"
                            }).done(function(returnData) {
                            if (returnData) {
                                if (returnData.BrokenRule !== null) {
                                    alert(returnData.BrokenRule.Message);
                                } else if (returnData.recipePosition !== null) {
                                    var bFound = false;
                                    //Done like this because you can only move backwards. Originally the logic was fuller but, most things don't change now.
                                    //The extra logic just hides everything past the active one
                                    for (var pos = 0; pos < viewModel.Stages().length; pos++) {
                                        if (viewModel.Stages()[pos].RecipeName() !==
                                            returnData.recipePosition.RecipeName)
                                            continue;

                                        for (var innerPos = 0;
                                            innerPos < viewModel.Stages()[pos].RecipeStages().length;
                                            innerPos++) {

                                            var recipeStage = viewModel.Stages()[pos].RecipeStages()[innerPos];

                                            if (bFound) {
                                                recipeStage.StepId(-1);
                                                recipeStage.IsNext(false);
                                            } else if (viewModel.Stages()[pos].Id() === returnData.ProcessStep.Id) {
                                                recipeStage.StepId(-1);
                                                recipeStage.IsNext(true);

                                                bFound = true;
                                            }

                                        }
                                    }
                                }
                            }
                        }).fail(function(xhr) {
                            try {
                                console.log(xhr.statusText);
                                console.log(xhr.responseText);
                                alert(xhr.statusText + "\r\n" + xhr.responseText);
                            } catch (ex) {
                                console.log(ex);
                                alert(ex);
                            }
                        });
                    }
                })
            });

If anyone can give me some guidance, that much be much appreciated. Thank you very much.

sinfella
  • 246
  • 2
  • 19

1 Answers1

0

It can be done simpler, here's an universal concept: How do you handle multiple submit buttons in ASP.NET MVC Framework?

Simple approach:

  1. multiple submit button, same name (let it be abcd), different value
  2. inside .NET controllers postback function have a string name (so string abcd) input parameter, where you check the value
baHI
  • 1,510
  • 15
  • 20