プログラムにはパラメーターなしでこのWrappedJsonResult2UploadImageSmall()のみが必要であることを理解していますが、UploadImageでパラメーターを送信します。
このコードを変更するのは、1つのビューに2つのUploadImageを配置し、IDを変更するために2を指定した場所で、どこかで間違いを犯した可能性があるためです(Upload Imageの部分)。
エラー
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))
}
};
}