首页 > 解决方案 > 如何获取 data-id 发送到另一个控制器 mvc

问题描述

在此处输入图像描述

在此处输入图像描述

如何将数据 ID 发送到 ASP.NET MVC 中的另一个控制器?

员工提交表格,自动确定id和部门职位,然后发送给经理审批。

经理在此页面上单击批准或拒绝,并将表单发送给下一个人进行批准。这一步页面提示id=null不能运行。我应该怎么办?

但是谷歌后台显示我得到了这个Id,如何发送这个Id到action?

public PartialViewResult saveStatus(int id, string Status, string AddDate, string Remark)
{
    int approvalId;

    if (id == 0 && Session["AppId"] == null)
    {
        var staff = db.Staffs.Where(s => s.UserName == User.Identity.Name).FirstOrDefault();

        RequestForApproval ap = new RequestForApproval
                    {
                        RequestToStaffId = staff.Id,
                        RequestDate = DateTime.Now,
                    };

        db.RequestForApprovals.Add(ap);
        db.SaveChanges();

        Session["AppId"] = ap.ReimbursementID;
        approvalId = ap.Id;
    }
    else
    {
        approvalId = int.Parse(Session["AppId"].ToString());
    }

    ApprovalStatus temp = new ApprovalStatus
                {
                    Id = approvalId,
                    Remark = Remark,
                    AddDate = DateTime.Now
                };

    db.ApprovalStatuses.Add(temp);
    db.SaveChanges();

    var df = db.ApprovalStatuses.Where(s => s.Id == approvalId).ToList();
    return PartialView(df);
}

public JsonResult CreateStatus()
{
    List<ApprovalStatus> mv = new List<ApprovalStatus>();

    if(Session["AppId"] == null)
    {
        ViewBag.Ae = 0;
    }
    else
    {
        ViewBag.Ae = Session["AppId"];
        int approvalId = int.Parse(Session["AppId"].ToString());
        mv = db.ApprovalStatuses.Where(s => s.Id == approvalId).ToList();
    }

    return Json(mv);
}

public ActionResult AddRequestForApproval()
{
    // var staffUser = db.StaffPositions.Where(a => a.Staff.UserName == System.Web.HttpContext.Current.User.Identity.GetUserId());
    var rmid = Session["RmId"].ToString();

    if (string.IsNullOrEmpty(rmid))
    {
        return RedirectToAction("Index");
    }

    int reimbursementId = int.Parse(rmid);
    Session["RmId"] = null;

    var Res = db.Reimbursements.Find(reimbursementId);
    var managerId = GetByStaffId(Res.StaffId,reimbursementId);

    RequestForApproval temp = new RequestForApproval();
    temp.ReimbursementID = reimbursementId;
    temp.RequestDate = DateTime.Now;
    temp.RequestToStaffId = managerId;

    db.RequestForApprovals.Add(temp);
    db.SaveChanges();

    return RedirectToAction("Index");
}

看法:

@model Reimbursements.Models.RequesViewModel

@{
        ViewBag.Title = "Index";
        var add = Session["AppId"];
}

<h2>Index</h2>

<table class="table" id="table1">
        <tr>
            <th></th>
            <th>
                Staff Fname
            </th>
            <th>
                RequestDate
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model.GetReApproval)
        {
            <tr>
                <td>
                    <a href="~/Reimbursements/GetOtherDetails/@item.ReimbursementId">@item.ReimbursementId</a>
                </td>
                <td>
                    @item.StaffName

                </td>
                <td>
                    @item.RequestDate
                </td>
                <td>
                    <button type="button" class="btn btn-primary"  data-toggle="modal" data-id="@item.RequerForApprovalId" data-target="#exampleModal" id="SelectBtn">Select</button>

                    <div class="modal fade" id="exampleModal"  tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title" id="Title">Select and Confirm</h4>
                                </div>
                                <div class="modal-body">
                                    <form>
                                        <div class="form-group" >
                                            <input type="hidden" id="Reid" />
                                            @Html.DropDownList("ApprovalStatus", null, new { @class = "btn btn-info",id="enumId" })
                                            @*@Html.DropDownList("Index", ViewBag.DropDownList as SelectList,null, new { @class = "btn btn-info",@id="DropDownList" })*@
                                        </div>
                                        <hr />

                                        <div class="form-group" style="visibility:visible" id="remarktext">
                                            <label for="message-text" class="control-label">Remark:</label>
                                            <textarea class="form-control" id="message-text" ></textarea>
                                        </div>

                                    </form>
                                </div>
                                <div class="modal-footer">
                                    @*<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Confirme</button>*@
                                   <button data-id="@item.ReimbursementId" class="btn btn-primary" id="Submit" @*onclick="location.href='@Url.Action("AddRequestForApproval","Reimbursements")'"*@>Submit</button>
                                    <button class="btn btn-default" data-dismiss="modal" type="reset" id="Clear">Close</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </td>
            </tr>
        }
    </table>
    @section Scripts {
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#table1").on('click', '#SelectBtn', function () {
                    $('#Submit').click('#enumId', function () {
                        var bid = $(this).attr('data-id');
                        var status = $("#enumId option:selected").val();
                        var mess = $('#message-text').val();
                        var Rmid;
                        console.log(bid, status, mess);
                       // var b = $("#NewId");
                        @*if ($("#NewId").val() == undefined) {
                            Rmid = 0;
                        } else {
                            Rmid = $("#NewId").val();
                        } 
            $.ajax({
                type: 'POST',
                dataType: 'html',
                url: '@Url.Action("saveStatus")',
                data: { id: bid, status: status, Remark: mess },
                 success: function (data) {
                     console.log(data);
                     status.val('').url(data);
                     mess.val('');

                }, error: function (data) {
                      alert("error");
                        },
            })*@
                  })
                })
            })

        </script>
    }

标签: c#jqueryhtmlajaxasp.net-mvc

解决方案


而不是放在data-id那里,您可以将其作为隐藏文件的值放入表单中,以便在提交表单时发布:

<input type = "hidden" name="id" value="@item.ReimbursementId" />

如果在单击不同按钮时值可能会发生变化,您希望发送不同的值,那么您可以隐藏输入并在提交之前设置其值:

$('#Submit').click('#enumId', function () {
    var bid = $(this).data('id');
    $('[name="id"]').val(bid);
    // rest of your code....
});

编辑:如果您要使用 ajax 发布它,请注意您应该获得如下数据:

$(this).data('id');

推荐阅读