I have the following model class:
public class ItemClass
{
public int TMSServerID {get;set;}
public byte[] Timestamp {get;set;}
}
and inside my razor view i wrote the following, to display a checkbox that have its value as the objectid + timestamp
:
<input type="checkbox" class="checkBoxClass" name="CheckBoxSelection"
value="@(item.TMSServerID.ToString() + "~" + item.timestamp.ToString())"
/>
but when I check the page source for my IE browser, I found the following:
<input type="checkbox" class="checkBoxClass" name="CheckBoxSelection" value="243~System.Byte[]"
/>
so it seems that Razor view did not concatenate the timestamp value, and instead it concatenate the timestamp type which is byte[] ?
Edit
Now I have the following jquery which will capture the concatenated string and send it ot the action method:
var boxData = [];
$("input[name='CheckBoxSelection']:checked").each(function () {
boxData.push($(this).val());
});
}
$.ajax({
type: "POST",
url: URL,
data: { ids: boxData.join(",")}
//code goes here
and action method is:
public ActionResult TransferSelectedServers(string ids, int? rackTo)
{
if (ModelState.IsValid)
{
try
{
var serverIDs = ids.Split(',');
int i = 0;
foreach (var serverinfo in serverIDs)
{
var split = serverinfo.Split('~');
var name = split[0];
//System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] bytearray = Encoding.Default.GetBytes(split[1]);
i++;
var server = repository.FindServer_JTechnology(Int32.Parse(name));
if (server == null)
return Json(new { IsSuccess = false, reload = true, description = " Some Servers might have been deleted, Transferre process has been cancelled .", rackid = rackFrom }, JsonRequestBehavior.AllowGet);
server.RackID = rackTo;
string ADusername = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1);
repository.InsertOrUpdateServer(server, ADusername, server.Technology.IT360ID.Value, server.IT360SiteID, new bool(), server.Technology,bytearray);
}
repository.Save();
return Json(new { IsSuccess = true, description = i + " Server/s Transferred Successfully To Rack " + }, JsonRequestBehavior.AllowGet);
}
catch (DbUpdateConcurrencyException e)
{
return Json(new { IsSuccess = false, reload = true, description = "records has been modified by another user" }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json(new { IsSuccess = false, reload = true, description = " Server/s Can not Be Transferred to the Selected Rack " }, JsonRequestBehavior.AllowGet);
}
}
return RedirectToAction("Details", new { id = rackTo });
and the repository method is :-
public void InsertOrUpdateServer(TMSServer server, string username, long assetid, long? siteid = 0, bool isTDMHW = false, Technology t = null,byte[] timestamparray)
{
server.IT360SiteID = siteid.Value;
tms.Entry(server).Property(s => s.timestamp).OriginalValue = timestamparray;
tms.Entry(server).State = EntityState.Modified;
var technology = tms.Technologies.Single(a => a.TechnologyID == server.TMSServerID);
technology.IsManaged = t.IsManaged;
tms.Entry(technology).State = EntityState.Modified;
InsertOrUpdateTechnologyAudit(auditinfo);
}
But currently I always get the following exception, even when I try editing the object by one user at a time:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries
}