1

我知道程序只需要这个 WrappedJsonResult2 UploadImageSmall() 没有参数,但我在 UploadImage 中发送参数。

我更改了此代码,因为我想在一个视图中有两个 UploadImage 并且在任何地方我都给了 2 来更改 id,也许我在某个地方犯了错误(在 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))
                }
            };
        }
4

2 回答 2

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/

希望它是你想要的。

于 2013-03-25T23:20:13.123 回答
0

这是问题-> public WrappedJsonResult2 UploadImageSmall(HttpPostedFileWrapper imageFile2)我忘记更改 imageFile 名称,因为在 JSON 事件中必须获得相同的参数名称。

于 2013-03-26T10:55:07.813 回答