Reputation: 183
I have a table "recipientMap". This table looks like that :
| MapID | IPgroupID | PCgroupID | AlertID
|:------|----------:|:---------:|:------:
| 1 | 1 | 1 | 15
| 2 | 3 | null | 15
| 3 | 3 | null | 16
This table helps me to know who can access to a message.
This choice is done when a user write the alert from the Mvc application, with a checkbox list for each. So I built an array for checked IP group id's and for checked PC group id's and push both into a new multidimensional array. I send this array to the controller. Next step is to iterate on it, and built a list based on RecipientMap model.
The problem is when one of the list has more or less checked id. For example, If I have in my multidimensional array a result like that :
list[0][0]=1
list[0][1]=3
list[1][0]=1
I get an error because there is no list[1][1]
. My list must have the same length... it's not good, and I know it's because my iteration method is not correct, I need some advice to correct it.
List[0][x]
is for IP and List[1][x]
is for PC. My array could have one or both list.
$('#bttn').click(function () {
var brutelistip = null;
brutelistip = [];
var brutelistpc = null;
brutelistpc = [];
$('#AlertCreationTabs #AlertCreationTabs-1 input:checkbox:checked').each(function () {
brutelistip.push($(this).attr('value'));
});
$('#AlertCreationTabs #AlertCreationTabs-2 input:checkbox:checked').each(function () {
brutelistpc.push($(this).attr('value'));
});
var recipientlist = new Array();
recipientlist[0] = brutelistip;
recipientlist[1] = brutelistpc;
$.ajax({
url: '/Panels/CreateNewAlert',
dataType: 'json',
type: 'post',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({
...
list: recipientlist,
}),
async: false,
processData: true,
success: function (data) {
alert(data);
},
error: function (xhr) {
alert('error');
}
});
});
And the controller :
public JsonResult CreateNewAlert(AlertMap alertMap, AlertLog alertLog, RecipientMap recipientMap, int[][] list)
{
if (ModelState.IsValid)
{
...
var RM = new RecipientMap();
List<RecipientMap>reciplist = new List<RecipientMap>();
for (int i = 0; i < list.Length; i++)
{
reciplist.Add(new RecipientMap
{
IPgroupID = list[0][i],
PCgroupID = list[1][i]
});
}
AL.RecipientMap.AddRange(reciplist);
AL.AlertMap.Add(AM);
db.AlertLog.Add(AL);
db.SaveChanges();
}
...
}
Upvotes: 1
Views: 50
Reputation: 183
Thanks, I found a solution ! First of all I wish to keep [0] for ip list and [1] for pc list. So I'v added a if statement, for push 0 if there is no checked item.
if ($('#AlertCreationTabs #AlertCreationTabs-1 input:checkbox:checked').length > 0)
{
$('#AlertCreationTabs #AlertCreationTabs-1 input:checkbox:checked').each(function () {
brutelistip.push($(this).attr('value'));
});
} else {
brutelistip.push(0);
}
if ($('#AlertCreationTabs #AlertCreationTabs-2 input:checkbox:checked').length > 0)
{
$('#AlertCreationTabs #AlertCreationTabs-2 input:checkbox:checked').each(function () {
brutelistpc.push($(this).attr('value'));
});
} else {
brutelistpc.push(0);
}
And then in the controller
if (list[0][0]!=0)
{
for (int i = 0; i < list[0].Length; i++)
{
reciplist.Add(new RecipientMap
{
IPgroupID = list[0][i],
});
}
}
if (list[1][0]!=0)
{
for (int j = 0; j < list[1].Length; j++)
{
reciplist.Add(new RecipientMap
{
PCgroupID = list[1][j],
});
}
}
I know that repeting code is not a very good practice, but... it's enough for the moment.
Upvotes: 1
Reputation: 743
Assuming both list[0]
and list[1]
are the same length, then it may be wise to replace list.Length
with list[0].Length
:
for (int i = 0; i < list[0].Length; i++)
{
reciplist.Add(new RecipientMap
{
IPgroupID = list[0][i],
PCgroupID = list[1][i]
});
}
If they're not the same length, you're going to have to do a conditional check before accessing one or the other. Either solution is a bit fiddly but they both work for their different circumstances.
Where one exists but the other does not, you could insert a null
into one of the RecipientMap
properties to create the List<RecipientMap>
element.. or you could not create it at all. It's entirely up to your implementation.
Upvotes: 0