gpt4 book ai didi

javascript - 将 JS 数组传递回 Controller 问题

转载 作者:行者123 更新时间:2023-12-02 18:59:57 26 4
gpt4 key购买 nike

我认为:

  <% using (Html.BeginForm("SaveRecommendedUserDetails", "Recommend", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<% foreach (var group in Model.Results)
{ %>
<div id="group-select<%: group.GroupId %>" style="width:auto; height:50px; border:solid 1px black; margin:5px;" >
<h3>&nbsp; &nbsp; <a href="javascript:OnGroupClicked(<%: group.GroupId %>)" id="changer<%: group.GroupId %>"><%: group.Name %></a></h3>
</div>
<% } %>
{

JS:

 <script type="text/javascript">   
function OnGroupClicked(groupId) {
var groupIds = new Array();
$("#changer" + groupId).on("click", function (e) {
e.preventDefault();
var body = $('#group-select' + groupId),
green = "rgb(0, 128, 0)",
white = "rgb(255, 255, 255)";

if (body.css("backgroundColor") !== green) {
groupIds.push(groupId);
body.css("backgroundColor", green);
} else {
groupIds.pop(groupId);
body.css("backgroundColor", white);
}
});
}
</script>

我的 Controller :

   [HttpPost]
public ActionResult SaveRecommendedUserDetails(RecommendedUserDataModel model)
{
var userId = ZincService.GetUserIdByEmail(model.Email);
model.UserId = userId;
ZincService.SaveRecommendedUserDetails(model);
return RedirectToAction("Index");
}

推荐的用户数据模型

public class RecommendedUserDataModel
{
public int UserId { get; set; }
public string Email { get; set; }
public string Firstname { get; set; }
public string Surname { get; set; }
public string Department { get; set; }
public string JobTitle { get; set; }

public IEnumerable<DataModels.Group.GroupDataModel> Groups { get; set; }
}

我的问题是,如果它被“选择”并点击,我需要将 groupId 返回到 Controller 。请问我该怎么做?

最佳答案

第三步:

   <script type="text/javascript">   
function OnGroupClicked(groupId) {
var groupIds = new Array();
$("#changer" + groupId).on("click", function (e) {
//Here is Step#3
$('#hdnGroupId').val(groupId+','+$('#hdnGroupId').val());

});
}
</script>

关于javascript - 将 JS 数组传递回 Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891627/

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