gpt4 book ai didi

javascript - 将按钮的值传递给隐藏字段并将其保存到数据库

转载 作者:行者123 更新时间:2023-11-28 03:17:02 26 4
gpt4 key购买 nike

所以我的设计是我有多个显示字段和一个隐藏字段。除了每一行之外,还有两个按钮接受和拒绝。我的观点是,当我单击按钮时,其值会传递到隐藏字段并在数据库中自动提交。当我尝试我的代码时,按钮没有传递任何内容,我尝试了与此主题相关的所有解决方案,但没有任何效果。我究竟做错了什么?

Controller :

    [HttpGet]
public ActionResult Add_Fulfillment ()
{
var getData = db.TBL_Request.Include(x=>x.TBL_Accounts).Include(x=>x.TBL_Accounts_LOBs).ToList() ;
var add_fulfillment = new Add_Fulfillment();
var ful_ = new fulfillmentVM();
foreach (var data in getData)
{
List<Add_Fulfillment> fulfillment = db.TBL_Request.Select(i => new Add_Fulfillment
{
Request_ID = i.Request_ID,
Employee_no = i.Employee_no,
Operation_Date = i.Operation_Date,
Fulfillment_Rate = i.Fulfillment_Rate ??0,
Account_Name = i.TBL_Accounts.Account_Name,
LOB = i.TBL_Accounts_LOBs.LOB,
Status = i.Inserted_by
}).ToList();
ful_._Requests = fulfillment;
}


return View(ful_);
}

[HttpPost]
public ActionResult Add_Fulfillment_Accept(int Request_ID , int? Status)
{
var user= db.TBL_Request.Find(Request_ID);


//hiddenfieldvalue assigns it to the field in the database i want to update

db.SaveChanges();
return RedirectToAction("Add_Fulfillment");
}
[HttpPost]
public ActionResult Add_Fulfillment_Reject(int Request_ID)
{
return RedirectToAction("Add_Fulfillment");
}

View

@model Staff_Requisition.Models.fulfillmentVM

@{
ViewBag.Title = "Add_Fulfillment";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Add_Fulfillment</h2>

<!-- page content -->


<div class="right_col" role="main">
<div class="">
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Plain Page</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<table class="table">
<tr>
<th>
Employee_no
</th>
<th>
Operation_Date
</th>
<th>
Fulfillment_Rate
</th>
<th>
Account_Name
</th>
<th>
LOB
</th>
<th></th>
<th></th>
</tr>

@for (int i = 0; i < Model._Requests.Count(); i++)
{
<tr>

<td>
@Html.DisplayFor(x => Model._Requests[i].Employee_no)
</td>
<td>
@Html.DisplayFor(x => Model._Requests[i].Operation_Date)
</td>
<td>
@Html.DisplayFor(x => Model._Requests[i].Fulfillment_Rate)
</td>
<td>
@Html.DisplayFor(x => Model._Requests[i].Account_Name)
</td>
<td>
@Html.DisplayFor(x => Model._Requests[i].LOB)
</td>
<td>

@Html.Hidden("Status" , Model._Requests[i].Status , new { id = "myEdit" })

</td>
@using (Html.BeginForm("Add_Fulfillment_Accept", "TBL_Request", FormMethod.Post, new { @id = "myForm" }))
{
@Html.AntiForgeryToken()
<td>
<button id="btnAccept" class="btn btn-lg btn-success" name="a_button" type="submit" value="122">Accept</button>
@Html.Hidden("Request_ID", Model._Requests[i].Request_ID)
</td>
}
@using (Html.BeginForm("Add_Fulfillment_Reject", "TBL_Request", FormMethod.Post, new { @id = "myForm" }))
{
@Html.AntiForgeryToken()
<td>
<button id="btnReject" class="btn btn-lg btn-danger" name="button" type="submit" value="222">Reject</button>
@Html.Hidden("Request_ID", Model._Requests[i].Request_ID)
</td>
}
</tr>

}

</table>

</div>
</div>
</div>
</div>
</div>
</div>

<!-- /page content -->


@section Scripts {
<script>

$("#btnAccept").click(function () {
$("#myEdit").val($("#btnAccept").val());

})

$("#btnReject").click(function () {
$("#myEdit").val($("#btnReject").val());

})


</script>
}

最佳答案

我解决了。唯一的问题是我没有将状态的隐藏字段放入表单中。这就是为什么它永远不会传递该值。不过还是感谢您的所有努力!查看:


@using (Html.BeginForm("Add_Fulfillment_Accept", "TBL_Request", FormMethod.Post))
{
@Html.AntiForgeryToken()
<td>
<button id="btnAccept" class="btn btn-lg btn-success" name="a_button" type="submit" onclick="accept(122)" value="122">Accept</button>
@Html.Hidden("Request_ID", Model._Requests[i].Request_ID)
@Html.Hidden("Status", Model._Requests[i].Status, new { id = "myEdit", value = "" })
</td>
}
@using (Html.BeginForm("Add_Fulfillment_Reject", "TBL_Request", FormMethod.Post))
{
@Html.AntiForgeryToken()
<td>
<button id="btnReject" class="btn btn-lg btn-danger" name="button" type="submit" onclick="reject(222)" value="222">Reject</button>
@Html.Hidden("Request_ID", Model._Requests[i].Request_ID)
@Html.Hidden("Status", Model._Requests[i].Status, new { id = "myEdit", value = "" })
</td>
}
</tr>

}

</table>

</div>
</div>
</div>
</div>
</div>
</div>

<!-- /page content -->


@section Scripts {
<script>
$('[name = "a_button"]').click(function () {
$('[name = "Status"]').val($('[name = "a_button"]').val());

})

$('[name = "button"]').click(function () {
$('[name = "Status"]').val($('[name = "button"]').val());

})


</script>

关于javascript - 将按钮的值传递给隐藏字段并将其保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59542892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com