无参数构造函数上传图像
本文关键字:图像 构造函数 参数 | 更新日期: 2023-09-27 18:05:43
我理解程序只需要这个WrappedJsonResult2 UploadImageSmall()没有参数,但我在UploadImage发送参数。
我改变这段代码,因为我想有两个UploadImage在一个视图和无处不在我给了2改变id,也许我在某处犯了错误(在上传图像部分),但我真的不知道在哪里可以与这个问题。
图片上传教程
误差 No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +199
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +572
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
上传图片@using (Html.BeginForm("UploadImageSmall", "StronaGlowna", FormMethod.Post,
new
{
enctype = "multipart/form-data",
id = "ImgForm2",
name = "ImgForm2",
target = "UploadTarget"
}))
{
<input type="file" name="imageFile2" />
<input type="button" value="Zapisz" onclick="UploadImage2()" />
}
<iframe id="UploadTarget2" name="UploadTarget2" onload="UploadImage_Complete2();" style="position: absolute;
left: -999em; top: -999em;"></iframe>
<div id="Images2">
</div>
<script type="text/javascript">
var isFirstLoad2 = true;
function UploadImage2() {
$("#ImgForm2").submit();
}
function UploadImage_Complete2() {
//Check to see if this is the first load of the iFrame
if (isFirstLoad2 == true) {
isFirstLoad2 = false;
return;
}
//Reset the image form so the file won't get uploaded again
document.getElementById("ImgForm2").reset();
//Grab the content of the textarea we named jsonResult . This shold be loaded into
//the hidden iFrame.
var newImg2 = $.parseJSON($("#UploadTarget2").contents().find("#jsonResult2")[0].innerHTML);
//If there was an error, display it to the user
if (newImg2.IsValid == false) {
alert(newImg2.Message);
return;
}
//Create a new image and insert it into the Images div. Just to be fancy,
//we're going to use a "FadeIn" effect from jQuery
var imgDiv2 = document.getElementById("Images2");
var img2 = new Image();
img2.src = newImg2.ImagePath;
//Hide the image before adding to the DOM
$(img2).hide();
imgDiv2.appendChild(img2);
//Now fade the image in
$(img2).fadeIn(500, null);
}
</script>
JSON [HttpPost]
public WrappedJsonResult2 UploadImageSmall(HttpPostedFileWrapper imageFile)
{
if (imageFile == null || imageFile.ContentLength == 0)
{
return new WrappedJsonResult2
{
Data = new
{
IsValid = false,
Message = "No file was uploaded.",
ImagePath = string.Empty
}
};
}
var fileName = String.Format("{0}.jpg", Guid.NewGuid().ToString());
var imagePath = Path.Combine(Server.MapPath(Url.Content("~/Content/UserImages")), fileName);
imageFile.SaveAs(imagePath);
var model = new StronaGlowna();
if (!TryUpdateModel(model))
{
}
model.MaleZdjecie = String.Format("~/Content/UserImages/{0}", fileName);
return new WrappedJsonResult2
{
Data = new
{
IsValid = true,
Message = string.Empty,
ImagePath = Url.Content(String.Format("~/Content/UserImages/{0}", fileName))
}
};
}
您可以尝试使用分部视图。下面的链接:
http://highoncoding.com/Articles/638_Understanding_Partial_Views_in_ASP_NET_MVC_Application.aspx http://rachelappel.com/razor/partial-views-in-asp-net-mvc-3-w-the-razor-view-engine/希望这是你想要的
这里有问题-> public WrappedJsonResult2 UploadImageSmall(HttpPostedFileWrapper imageFile2)我忘记了更改imageFile名称,因为在JSON事件中必须获得相同的参数名称。