问题描述
嘿,所以我有一个模式弹出窗体,在该窗体中,我必须上传文件并将数据发布到数据库。在控制器中,我通过FormCollection检索值。当我尝试使用表单集合获取输入字段时,出现以下错误:无法将System.Windows.Forms.Form隐式转换为'String'。这是我的代码:
控制器
DECLARE @Test VARCHAR(MAX) = '\\asda.xxv.ww\data\tas\Test\Clients\asdas\TestFolder\QA'
DECLARE @Test2 VARCHAR(MAX) = '\\uxcvs.xcxcv.xcv\data\tas\Test\Clients\xcvcx\xcvxcv\QA\'
SELECT RIGHT(SUBSTRING(@Test,2,LEN(@Test)-2),CHARINDEX('\',SUBSTRING(REVERSE(@Test),LEN(REVERSE(@Test))-2),(CHARINDEX('\',LEN(REVERSE(@Test))-2))))) AS DiconFilePath,RIGHT(SUBSTRING(@Test2,LEN(@Test2)-2),SUBSTRING(REVERSE(@Test2),LEN(REVERSE(@Test2))-2),LEN(REVERSE(@Test2))-2))))) AS DiconFilePath2
-----------------------,patindex('%\%',reverse(@Test)),reverse(@Test2))
----------------,case
when patindex('%\%',reverse(@Test)) > 1 THEN
reverse(LEFT(reverse(@Test),reverse(@Test)) - 1))
else
@Test
end,case
WHEN patindex('%\%',reverse(@Test2)) = 1 and len(@Test2) > 1 then
right(reverse(@Test2),LEN(reverse(@Test2)) - 1)
ELSE
@Test2
end,reverse(@Test2)) = 1 and len(@Test2) > 1 then
reverse(substring(right(reverse(@Test2),LEN(reverse(@Test2)) - 1),right(reverse(@Test2),LEN(reverse(@Test2)) - 1))))
ELSE
@Test2
end
HTML格式
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FormCollection formCollection,HttpPostedFileBase upload,AAC_procedure_document_types model,Ngarkodokument ngarkodok)
{
try
{
if (ModelState.IsValid)
{
if (upload != null && upload.ContentLength > 0)
{
var file = new AAC_procedure_documents
{
Emer_Dokumenti = System.IO.Path.GetFileName(upload.FileName),Lloji_File = model.Emri_llojit,Content_Type = upload.ContentType
};
using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
file.Permbajtje_Dokumenti = reader.ReadBytes(upload.ContentLength);
}
ngarkodok.AAC_procedure_documents = new List<AAC_procedure_documents> { file };
}
AAC_procedure_documents_location lokacion = new AAC_procedure_documents_location();
lokacion.Rafti = formCollection["Rafti"];
lokacion.Zyra = formCollection["Zyra"].ToString();
lokacion.Nr_Kutise = Convert.ToInt32(formCollection["Nr_Kutise"]);
db.AAC_procedure_documents_location.Add(lokacion);
db.SaveChanges();
return RedirectToAction("Dokumenti");
}
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("","Unable to save changes. Try again,and if the problem persists see your system administrator.");
}
return View(formCollection);
}
解决方法
FormCollection的命名空间必须是System.Web.Mvc,而不是System.Windows.Forms。查看您的用法并删除System.Windows.Forms ...
FormCollection类存在于WindowsForm和Web两个技术中。