发布文件在控制器中始终为null [英] Posted file getting always null in controller

查看:62
本文介绍了发布文件在控制器中始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模特是



 [RegularExpression(@([a-zA-Z0-9 \ s_\\。 \  - :])+(。png | .jpg | .gif)$,ErrorMessage =只允许图像文件。)] 
public HttpPostedFileBase Logo {get;组; }





我的部分视图是



 @ Html.ValidationSummary(true)
@using SocialMediaAnalytics.Web.Models;
@model SocialMediaAnalytics.Web.Models.Account.RegistrationModel
@using(Html.BeginForm(CreateDatabase,Settings,FormMethod.Post,new {id =formAddDatabaseandWorkSpace,encType =multipart / form-data}))
{
< div class =col-sm-12>
< div class =form-group>
< label>徽标< / label>
@ Html.TextBoxFor(m => m.Logo,new {type =file})
< / div>
< / div>
< / div>
< / div>
< div class =modal-footer>
< button type =buttonclass =btn btn-defaultdata-dismiss =modal>关闭< / button>
< button type =submitclass =btn btn-primaryid =btnAddDB> Save< / button>

< / div>
}







和控制器是

 [HttpPost] 
public JsonResult CreateDatabase(RegistrationModel model)
{
try
{
else if(model.Logo!= null)
{
LogoUpload(model);
返回Json(新{title =上传徽标,
message =徽标上传成功,类型=成功});

}
}
< / pre

但是模型中Logo的值即使在浏览图像后也始终为null。

我尝试了什么:

我尝试使用类型而不是@Html.TextBoxFor(),但它每次都变为空。

解决方案

,ErrorMessage =只允许图像文件。)]
public HttpPostedFileBase Logo {get; set;}





我的部分视图是



 @ Html.ValidationSummary (true)
@using SocialMediaAnalytics.Web.Models;
@model SocialMediaAnalytics.Web.Models.Account.RegistrationModel
@using(Html.BeginForm(CreateDatabase,Settings,FormMethod .Post,new {id =formAddDatabaseandWorkSpace,encType =multipart / form-data}))
{
< div class =col-sm-12>
< div class =form-group>
< label>徽标< / label>
@ Html.TextBoxFor(m => m.Logo,new {type =file} )
< / div>
< / div>
< / div>
< / div>
< div class =modal-footer >
<按钮类型e =buttonclass =btn btn-defaultdata-dismiss =modal>关闭< / button>
< button type =submitclass =btn btn-primaryid =btnAddDB> Save< / button>

< / div>
}







和控制器是

 [HttpPost] 
public JsonResult CreateDatabase(RegistrationModel model)
{
try
{
else if(model.Logo!= null)
{
LogoUpload(model);
返回Json(新{title =上传徽标,
message =徽标上传成功,类型=成功});

}
}
< / pre

但是模型中Logo的值即使在浏览图像后也始终为null。

我尝试了什么:

我尝试使用类型而不是@Html.TextBoxFor(),但它每次都变为空。


My model is

[RegularExpression(@"([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg|.gif)$", ErrorMessage = "Only Image files allowed.")]
		public HttpPostedFileBase Logo { get; set; }



my partial view is

@Html.ValidationSummary(true)
@using SocialMediaAnalytics.Web.Models;
@model SocialMediaAnalytics.Web.Models.Account.RegistrationModel
@using (Html.BeginForm("CreateDatabase", "Settings", FormMethod.Post, new { id = "formAddDatabaseandWorkSpace", encType = "multipart/form-data" }))
{
<div class="col-sm-12">
				<div class="form-group">
					<label>Logo</label>
					@Html.TextBoxFor(m => m.Logo, new { type = "file" })
				</div>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
		<button type="submit" class="btn btn-primary" id="btnAddDB">Save</button>

	</div>
}




and the controller is

[HttpPost]
		public JsonResult CreateDatabase(RegistrationModel model)
		{
			try
			{
                          else if(model.Logo!=null)
					{
					LogoUpload(model);
					return Json(new { title = "Upload Logo", 
                         message = "Logo uploaded successfully", type = "Success" });
						
					}
}
</pre

but the value for "Logo" in model is always null even after browsing the image.

What I have tried:

I have tried with  type instead of @Html.TextBoxFor() but its getting null everytime.

解决方案

", ErrorMessage = "Only Image files allowed.")] public HttpPostedFileBase Logo { get; set; }



my partial view is

@Html.ValidationSummary(true)
@using SocialMediaAnalytics.Web.Models;
@model SocialMediaAnalytics.Web.Models.Account.RegistrationModel
@using (Html.BeginForm("CreateDatabase", "Settings", FormMethod.Post, new { id = "formAddDatabaseandWorkSpace", encType = "multipart/form-data" }))
{
<div class="col-sm-12">
				<div class="form-group">
					<label>Logo</label>
					@Html.TextBoxFor(m => m.Logo, new { type = "file" })
				</div>
			</div>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
		<button type="submit" class="btn btn-primary" id="btnAddDB">Save</button>

	</div>
}




and the controller is

[HttpPost]
		public JsonResult CreateDatabase(RegistrationModel model)
		{
			try
			{
                          else if(model.Logo!=null)
					{
					LogoUpload(model);
					return Json(new { title = "Upload Logo", 
                         message = "Logo uploaded successfully", type = "Success" });
						
					}
}
</pre

but the value for "Logo" in model is always null even after browsing the image.

What I have tried:

I have tried with  type instead of @Html.TextBoxFor() but its getting null everytime.


这篇关于发布文件在控制器中始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆