【问题标题】:Can not implicitly convert System.Windows.Forms.Form to 'String'无法将 System.Windows.Forms.Form 隐式转换为“字符串”
【发布时间】:2020-12-17 11:56:27
【问题描述】:

嘿,所以我有一个模式弹出表单,我必须在其中上传文件并将数据发布到数据库。在控制器中,我通过 FormCollection 检索值。当我尝试使用表单集合获取输入字段时,出现此错误:无法将 System.Windows.Forms.Form 隐式转换为“字符串”。这是我的代码:

控制器

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

HTML格式

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Ngarkoni dokumenta</h4>
            </div>
            <div class="modal-body">
                @using (Html.BeginForm("Create", "NgarkoDokument", FormMethod.Post, new { enctype = "mulptiple/form-data" }))
                {
                    @Html.AntiForgeryToken()

                    <div class="form-group">
                        <label for="exampleFormControlSelect1">Lloji i dokumentit</label><br />
                        <select title="Lloji i dokumentit" name="lloji" class="form-control col-md-3 box" id="tipiDropdown"> </select>


                        <input type="button" title="Ngarko dokument" name="ngarko" value="Ngarko" id="uploadPop" class="btn btn-info col-md-3" onclick="document.getElementById('file').click();" />
                        <input type="file" onchange="javascript: updateList()" multiple="multiple" style="display:none;" id="file" name="postedFiles" />
                        <div id="fileList"></div>
                    </div>
                    <br /><br />

                    <div class="form-group">
                        <label for="formGroupExampleInput">Fusha indeksimi</label> <br />
                        @*<input title="Indeksimi dokumentit" id="indeksimi" class="form-control col-md-3" type="text" name="indeksimi" placeholder="indeksimi" required />*@
                        @Html.TextBoxFor(a => a.Fusha_Indeksimit.Emri_Indeksimit, new { @class = "form-control", @placeholder = "indeksimi" })

                        <button title="Shto indeksim" id="modalPlus" type="submit" class="btn btn-info"><i class="glyphicon glyphicon-plus"></i></button>


                    </div>

                    <label for="formGroupExampleInput">Vendndodhja fizike e dokumentit</label><br>
                    <div id="zyraModal" class="form-group col-md-4">
                        @*<input title="Zyra fizike" id="zyra" class="form-control" type="text" name="zyra" placeholder="Zyra" />*@
                        @Html.TextBoxFor(a => a.Vendndodhja_fizike.Zyra, new { @class = "form-control", @placeholder = "Zyra" })
                    </div>

                    <div class="form-group col-md-4">
                        @* <input title="Kutia fizike" id="kutia" class="form-control" type="number" name="kutia" placeholder="Nr i kutisë" />*@
                        @Html.TextBoxFor(a => a.Vendndodhja_fizike.Nr_Kutise, new { @class = "form-control", @placeholder = "Nr i kutisë" })
                    </div>

                    <div class="form-group col-md-4">
                        @* <input title="Rafti fizik" id="rafti" class="form-control" type="text" name="rafti" placeholder="Rafti" />*@
                        @Html.TextBoxFor(a => a.Vendndodhja_fizike.Rafti, new { @class = "form-control", @placeholder = "Rafti" })
                    </div>

                    <br /><br />

                    <div class="row" id="ruaj">
                        <button value="Create" title="Ruaj dokumentin" type="submit" class="btn btn-success">Ruaj</button>
                    </div>
                }
            </div>
        </div>

【问题讨论】:

  • 哪一行显示有错误?你能从错误中得到堆栈跟踪吗?
  • 您在 WEB 应用程序中使用 WINDOWS.Forms 做什么?

标签: c# asp.net .net asp.net-mvc asp.net-mvc-5


【解决方案1】:

FormCollection 的命名空间必须是 System.Web.Mvc 而不是 System.Windows.Forms。看看你的 usings 并删除 System.Windows.Forms...

FormCollection 类存在于 WindowsForm 和 Web 两种技术中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2014-05-15
    • 2014-02-04
    相关资源
    最近更新 更多