我认为:
<% 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> <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>
我的控制员:
[HttpPost]
public ActionResult SaveRecommendedUserDetails(RecommendedUserDataModel model)
{
var userId = ZincService.GetUserIdByEmail(model.Email);
model.UserId = userId;
ZincService.SaveRecommendedUserDetails(model);
return RedirectToAction("Index");
}
RecommendedUserDataModel
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恢复到控制器,如果它被“选中”,那么点击它。我该怎么办呢?
答案 0 :(得分:1)
您可以通过临时保存逗号分隔组ID将其传递到隐藏字段。 这样做的步骤:
完成上述步骤后,您可以在后期操作中将commaseprated GroupIds添加到控制器中。
答案 1 :(得分:1)
步骤3:
<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>