我理解程序只需要这个没有参数的WrappedJsonResult2 UploadImageSmall(),但我在UploadImage中发送参数。
我更改了这段代码,因为我想在一个View中有两个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))
}
};
}
答案 0 :(得分:0)
您可以尝试使用部分视图。以下一些链接:
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/
希望你想要的东西。
答案 1 :(得分:0)
这是问题 - &gt; public WrappedJsonResult2 UploadImageSmall(HttpPostedFileWrapper imageFile2)我忘了更改imageFile名称,因为在JSON事件中必须得到相同的参数名称。