获取按钮ID并将ID传递给模型按钮ID

时间:2020-07-27 08:47:15

标签: javascript c# jquery model-view-controller razor

我有一个动态按钮,它具有Stepid属性。我想做什么 是单击按钮时捕获该属性,然后将相同的属性传递到我的模型中,并在模式中将StepId值分配为我的按钮ID。

我的按钮

 <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>

和我的情态

    <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>

还有我的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);
                            }
                        });
                    }
                })
            });

如果有人可以给我一些指导,请多多感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

这可以更简单地完成,这是一个通用概念:How do you handle multiple submit buttons in ASP.NET MVC Framework?

简单方法:

  1. 多个提交按钮,相同的名称(将其命名为abcd),不同的值
  2. .NET控制器内部的回发功能具有一个字符串名称(即字符串abcd)输入参数,您可以在其中检查该值
相关问题