我有一个 HTTP Post 操作,我将一个 HttpPostedFileBase 和一个 FormCollection 作为我的参数发布
我的控制器动作如下:
[HttpPost]
public ActionResult NewContent(HttpPostedFileBase postedFile,string username, FormCollection form)
{
if (postedFile != null)
{
HttpPostedFileBase postedFileCopy = postedFile;
postedFileCopy.InputStream.Position = 0;
Stream stream = postedFile.InputStream;
//avm.AddContent(postedFile, stream, "jpmcfeely");
string[] name = form.GetValues("name");
string[] author = form.GetValues("author");
string[] description = form.GetValues("description");
DateTime uploaddate = DateTime.Today;//form.GetValues("uploaddate");
DateTime expirydate = DateTime.Today.AddMonths(1);//form.GetValues("expirydate");
string[] participationpoints = form.GetValues("participationpoints");
string[] contenttypeid = form.GetValues("contenttypeid");
try
{
avm.AddContent(postedFile, stream, "jpmcfeely", name.ToString(), author.ToString(), description.ToString(), uploaddate, expirydate, Convert.ToInt32(participationpoints), Convert.ToInt32(contenttypeid));
}
catch (Exception ex)
{
return RedirectToAction("Index", "Admin");
}
}
然后,我的 ViewModel 中有一个方法,它将表单集合的详细信息添加到我的 sql 存储表中,如下所示:
public void AddContent(HttpPostedFileBase content, System.IO.Stream stream, string userName, string name, string author, string description, string uploadDate, string expiryDate, int participationPoints, int contentType)
{
string contentUri = "";
Content newContent = new Content();
contentUri = Helpers.ContentUtils.AddContentToBlob(content, stream, "useravatar", content.FileName);
newContent.Name = name;
newContent.Author = author;
newContent.Description = description;
newContent.UploadDate = DateTime.Today;
newContent.ExpiryDate = DateTime.Today.AddMonths(1);
newContent.ParticipationPoints = 50;
newContent.ContentBlobURL = contentUri;
newContent.ContentTypeID = contentType;
this.contentRepository.Add(newContent);
this.contentRepository.SaveChanges();
}
如果我构建解决方案,它编译时没有错误,但在控制器上的 try 块内保存内容时,我收到以下错误:
无法将“System.String[]”类型的对象转换为“System.IConvertible”类型
如果我只是通过硬编码传递字符串值,那么该方法会按预期保存,所以我的问题只是将它们转换为适当的格式。执行此操作的最佳方法是什么,或者我错过了一步?