I am trying to pass byte[] as value and a string as text to Html.DropDownList using SelectList. However the values associated with the text in the dropdown show up as "System.Byte[]". This causes model binding issues which results in the following exception:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
- I create a list of BAModels
- Inside the create Action I save the list as ViewBag.site_OID
- Then in the view I use the above item from ViewBag in the @Html.DropDownList
Model:
public class BAModel
{
public byte[] mfg_site_OID { get; set; }
public string mfg_site_id { get; set; }
}
Action:
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();
}
Relevant code from the View:
@{
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>
So it seems new SelectList(BAList, "mfg_site_OID", "mfg_site_id")
when translates to DropDownList is not understanding the byte[] and using the data type instead. Looking for help on how to fix this from anyone who have used byte[] as the value of the DropDownList items.