Request.Files.Count 始终为 0 MVC .net
Request.Files.Count is always 0 MVC .net
好的,所以我正在尝试在我的网页上上传文件,但是我 运行 遇到 Request.Files.Count > 0
始终为 0 并且不会输入 if 语句的问题。我已经尝试了多种方法来做到这一点,并在这个类似的问题中查看了多个答案(有很多)但是 none 的解决方案有效。
控制器
public class TicketController : CommonController
{
[HttpPost]
public async Task<ActionResult> Create(TicketViewModel model)
{
if (ValidateModel(model))
{
if (Request.Files.Count > 0) /*This is always 0*/
{
HttpPostedFileBase file = Request.Files[0];
}
}
}
}
查看
@using (Html.BeginForm("Create", "TicketController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-field">
@Html.TextBoxFor(model => model.FileUpload, new { type="file", name = "FileUpload"})
@Html.ValidationMessageFor(model => model.FileUpload)
</div>
}
型号
public class TicketViewModel
{
public HttpPostedFileBase FileUpload { get; set; }
}
尝试添加或删除和更改视图中的名称以及模型中的变量。但是,即使填满了,运气也总是归零。我的理解是,如果检测到请求密钥,它会更新变量以实际包含某些内容。所以我的视图和控制器断开连接,我无法弄清楚。任何方向将不胜感激。
编辑:
提交按钮
<div class="form-group bottom-button-row">
<div class="col-md-offset-2 col-lg-offset-2 col-sm-offset-4">
<div class="col-sm-12 btn-panel">
<div class="float-left">
<input type="button" value="Submit" class="btn btn-primary" data-bind="click:submit,disable:submitting" />
<a role="button" class="btn btn-default" href="@ViewBag.CancelUrl" data-bind="attr:{disabled:submitting}">Cancel</a>
</div>
<div class="loader" data-bind="visible: submitting" style="display: none;"></div>
</div>
</div>
</div>
原来另一个 CSHTML 文件已经在一个表单中调用这个 CSHTML 文件,所以我在一个不受支持的表单中嵌套了一个表单,我更新了外部表单,一切正常并且可以正常工作。评论你的代码的人!
好的,所以我正在尝试在我的网页上上传文件,但是我 运行 遇到 Request.Files.Count > 0
始终为 0 并且不会输入 if 语句的问题。我已经尝试了多种方法来做到这一点,并在这个类似的问题中查看了多个答案(有很多)但是 none 的解决方案有效。
控制器
public class TicketController : CommonController
{
[HttpPost]
public async Task<ActionResult> Create(TicketViewModel model)
{
if (ValidateModel(model))
{
if (Request.Files.Count > 0) /*This is always 0*/
{
HttpPostedFileBase file = Request.Files[0];
}
}
}
}
查看
@using (Html.BeginForm("Create", "TicketController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-field">
@Html.TextBoxFor(model => model.FileUpload, new { type="file", name = "FileUpload"})
@Html.ValidationMessageFor(model => model.FileUpload)
</div>
}
型号
public class TicketViewModel
{
public HttpPostedFileBase FileUpload { get; set; }
}
尝试添加或删除和更改视图中的名称以及模型中的变量。但是,即使填满了,运气也总是归零。我的理解是,如果检测到请求密钥,它会更新变量以实际包含某些内容。所以我的视图和控制器断开连接,我无法弄清楚。任何方向将不胜感激。
编辑: 提交按钮
<div class="form-group bottom-button-row">
<div class="col-md-offset-2 col-lg-offset-2 col-sm-offset-4">
<div class="col-sm-12 btn-panel">
<div class="float-left">
<input type="button" value="Submit" class="btn btn-primary" data-bind="click:submit,disable:submitting" />
<a role="button" class="btn btn-default" href="@ViewBag.CancelUrl" data-bind="attr:{disabled:submitting}">Cancel</a>
</div>
<div class="loader" data-bind="visible: submitting" style="display: none;"></div>
</div>
</div>
</div>
原来另一个 CSHTML 文件已经在一个表单中调用这个 CSHTML 文件,所以我在一个不受支持的表单中嵌套了一个表单,我更新了外部表单,一切正常并且可以正常工作。评论你的代码的人!