C# System.Web.Mvc.SelectList不';不能使用字节[]
我正在尝试使用SelectList将字节[]作为值和字符串作为文本传递给Html.DropDownList。但是,下拉列表中与文本关联的值显示为“System.Byte[]”。这会导致模型绑定问题,从而导致以下异常: 输入不是有效的Base-64字符串,因为它包含非Base-64字符串 字符、两个以上填充字符或非法字符 在填充字符中 描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源 异常详细信息:System.FormatException:输入无效 Base-64字符串,因为它包含一个非Base 64字符,多于两个 填充字符,或填充字符中的非法字符 人物C# System.Web.Mvc.SelectList不';不能使用字节[],c#,razor,asp.net-mvc-5,C#,Razor,Asp.net Mvc 5,我正在尝试使用SelectList将字节[]作为值和字符串作为文本传递给Html.DropDownList。但是,下拉列表中与文本关联的值显示为“System.Byte[]”。这会导致模型绑定问题,从而导致以下异常: 输入不是有效的Base-64字符串,因为它包含非Base-64字符串 字符、两个以上填充字符或非法字符 在填充字符中 描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源 异常详细信息:System.FormatExc
public class BAModel
{
public byte[] mfg_site_OID { get; set; }
public string mfg_site_id { get; set; }
}
行动:
public ActionResult Create()
{
//ViewBag.site_OID = new SelectList(db.mfg_site, "mfg_site_OID", "mfg_site_id");
var BAList = new List<BAModel>();
BAList.Add(new BAModel { mfg_site_OID= new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, mfg_site_id ="london" });
BAList.Add(new BAModel { mfg_site_OID = new byte[] { 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21 }, mfg_site_id = "new york" });
ViewBag.site_OID = new SelectList(BAList, "mfg_site_OID", "mfg_site_id");
return View();
}
public ActionResult Create()
{
//ViewBag.site_OID=新的选择列表(db.mfg_site,“mfg_site_OID”,“mfg_site_id”);
var BAList=新列表();
添加(新的BAModel{mfg_site_OID=新字节[]{0x20,0x20,0x20,0x20,0x20,0x20},mfg_site_id=“london”});
添加(新BAModel{mfg_site_OID=新字节[]{0x21,0x21,0x21,0x21,0x21,0x21},mfg_site_id=“纽约”});
ViewBag.site_OID=新的选择列表(BAList,“制造现场_OID”、“制造现场_id”);
返回视图();
}
视图中的相关代码:
@{
ViewBag.Title = "Create";
SelectList list = ViewBag.site_OID;
}
<div class="form-group">
@Html.LabelFor(model => model.site_OID, "site_OID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("site_OID", list, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.site_OID, "", new { @class = "text-danger" })
</div>
</div>
@{
ViewBag.Title=“创建”;
SelectList list=ViewBag.site\u OID;
}
@LabelFor(model=>model.site_-OID,“site_-OID”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“site_OID”,list,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.site_OID,“,new{@class=“text danger”})
因此,当转换为DropDownList时,new SelectList(BAList,“mfg\u site\u OID”,“mfg\u site\u id”)
似乎不理解字节[],而是使用数据类型。正在向使用byte[]作为DropDownList项值的任何人寻求有关如何修复此问题的帮助。答案是您不能。需要字符串值,所以如果确实要存储为字节数组,则需要转换为
public class BAModel
{
public string mfg_site_OID { get; set; }
public string mfg_site_id { get; set; }
}
mfg_site_OID = Convert.ToBase64String(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 })
然后在回发时将其转换回字节数组以获取原始值
如果您想了解更多关于MVC中DropDownList的信息,我建议您看看这个。您能不能不使用string?我用模拟模型简化了这个问题,但实际上我正在处理一个预先存在的SQL Server DB,它具有二进制类型的OID列。在.Net模型中,二进制变成字节[]。二进制列中存储了什么?该表没有任何主键列可用于绑定到dropdownlist?应该注意的是,这样做通常是一个非常糟糕的主意。使用byte[]作为值意味着您必须实际发送嵌入HTML文档中的所有数据。更糟糕的是,由于必须转换为Base64才能真正做到这一点,因此HTML中包含的数据将扩展大约150%,因为Base64将使用字符串中的多个字节对数组中的单个字节进行编码。根据这些字节数组的大小,您可以强制下载兆字节甚至千兆字节来呈现HTML文档。换句话说,不要。