问题描述
大家好,我正在尝试上传一个简单的图像,但是HttpPostedFileBase始终为null.这是我的代码,我不知道自己在做什么错.
Hi everyone i am trying to upload a simple image but the HttpPostedFileBase is always remaining null. This is my code i dont know what i am doing wrong.
这是我在设计视图中的代码:
This is my code in the design view:
<fieldset>
<legend>PictureModel</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.PrivacyTypeID) %>
</div>
<div class="editor-field">
<%: Html.DropDownList("PrivacyTypeID", null, new { name = "PrivacyTypeID", title = "Please select privacy type.", id = "PrivacyTypeID" }) %>
<%: Html.ValidationMessageFor(model => model.PrivacyTypeID) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.File1) %>
</div>
<div class="editor-field">
**<input type="file" name="File1" />**
<%: Html.ValidationMessageFor(model => model.File1) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Description) %>
</div>
这是我的控制器中的代码:
And here is the code in my controller:
public ActionResult AddPicture(向导ID,PictureModel模型,HttpPostedFileBase File1) { 尝试 {
public ActionResult AddPicture(Guid id, PictureModel model, HttpPostedFileBase File1) { try {
if (ModelState.IsValid)
{
try
{
Guid albumid = id;
if (File1 != null)
{
var physicalPath = Path.Combine(Server.MapPath("~/Gallery"), System.IO.Path.GetFileName(File1.FileName));
File1.SaveAs(physicalPath);
PicturesBL pictures = new PicturesBL();
任何人都可以告诉我这是什么问题吗?
Can anyone please tell me what is the problem??
推荐答案
这也使我发疯.几个小时后我意识到,
This was driving me crazy too. After hours I realized, that
@Scripts.Render( "~/bundles/jquerymobile")
_Layout.cshtml中的
导致了此问题.注释掉后,上传成功.
in _Layout.cshtml was causing the problem. When commented out, the upload worked.
请参见 http ://forum.jquery.com/topic/jquery-mobile-seems-to-clobber-ability-to-upload-files-via-forms 了解详情.
在表单中添加
data_ajax="false"
属性可以解决该问题.
这篇关于上传图片MVC始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!