无法在我的Razor视图中将我的Int ObjectId与我的Byte []时间戳连接起来

时间:2014-07-31 10:12:34

标签: asp.net-mvc razor asp.net-mvc-5

我有以下模型类:

public class ItemClass
{
    public int TMSServerID {get;set;}
    public byte[] Timestamp {get;set;}
}

在我的剃刀视图中,我编写了以下内容,以显示一个复选框,其值为objectid + timestamp

<input type="checkbox" class="checkBoxClass" name="CheckBoxSelection" 
                               value="@(item.TMSServerID.ToString() + "~" + item.timestamp.ToString())"
                                /> 

但是当我检查IE浏览器的页面源时,我发现了以下内容:

<input type="checkbox" class="checkBoxClass" name="CheckBoxSelection" value="243~System.Byte[]"
                                /> 

所以似乎Razor视图没有连接时间戳值,而是连接时间戳类型,即byte []?

修改

现在我有以下jquery,它将捕获连接的字符串并将其发送给action方法:

var boxData = [];

        $("input[name='CheckBoxSelection']:checked").each(function () {
            boxData.push($(this).val());
        });

    }

         $.ajax({
             type: "POST",
             url: URL,
             data: { ids: boxData.join(",")}
            //code goes here

和行动方法是:

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 });

,存储库方法是: -

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);

            }

但是目前我总是得到以下异常,即使我一次尝试由一个用户编辑对象:

  

存储更新,插入或删除语句会影响意外   行数(0)。自那以后,实体可能已被修改或删除   实体已加载。刷新ObjectStateManager条目

    }

1 个答案:

答案 0 :(得分:1)

尝试

.... + Convert.ToBase64String(item.timestamp as byte[])"

请注意,这是在HiddenInput

的MVC源代码中完成的

并将值转换回控制器中的Timestamp,使用

byte[] timeStamp = Convert.FromBase64String(yourValue)