1

我在下面的代码中提交了问题。在 Firebug 上,我收到以下消息:

POST localhost:9706/Home/Upload 500 内部服务器错误

没有为此对象定义无参数构造函数。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: System.MissingMethodException:没有为此对象定义无参数构造函数。

这个表单我可以提交但没有 jquery 和 unobtrusive-ajax 脚本,在这种情况下提交看起来像普通提交(整个页面提交,没有 ajax)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<FileInfoModel>>"%> 

 <%@ Import Namespace="MembershipTest.Models"%> 
<script src="../../Scripts/jquery-1.7.1.js"
type="text/javascript"></script>  
<script src="../../Scripts/jquery.unobtrusive-ajax.js"type="text/javascript"</script>   
<script type="text/javascript">     
function OnSuccess(response) {
     alert(response);
 }
 function OnFailure(response) {
     alert("Whoops! That didn't go so well did it?");  
 }   </script>   
<% using (Ajax.BeginForm("Upload", "Home", null,
     new AjaxOptions
     {
         HttpMethod = "POST",
         UpdateTargetId = "uploadTable",
         InsertionMode = InsertionMode.Replace,
         OnSuccess = "OnSuccess",
         OnFailure = "OnFailure" 
     },
     new { enctype = "multipart/form-data" })){%>  
  <fieldset> 
  <legend> Upload File: </legend> <span>Filename:</span>
  <input type="file" name="file" id="file" /> 
  <input type="submit" value="Upload" /> 
  </fieldset> <% } %>  
  <div id="uploadTable"></div>

控制器代码

    [HttpGet]
    [ChildActionOnly]
    public ActionResult Upload()
    {

        List<FileInfoModel> FilesInfoData =  new List<FileInfoModel>();

        DirectoryInfo dir = new DirectoryInfo(Server.MapPath(uploadLocation));
        var files = from f in dir.GetFiles()
                    select f;
        foreach (var i in files)
        {
            FileInfoModel fmodel = new FileInfoModel(){
                Name = i.Name,
                Length = i.Length,
                LastWriteTime = i.LastWriteTime
            };
            FilesInfoData.Add(fmodel);
        }

        return PartialView("Upload",FilesInfoData);
    }
    [HttpPost]
    public ActionResult Upload(HttpPostedFileWrapper file)
    {

        if (file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var saveLocation = Path.Combine(Server.MapPath(uploadLocation), fileName);
            file.SaveAs(saveLocation);
        }
        return RedirectToAction("Index");
    }

型号代码

public class FileInfoModel
{
    public FileInfoModel(){}
    public string Name { get; set; }
    public double Length { get; set; }
    public DateTime LastWriteTime { get; set; }       
}
4

2 回答 2

0

昨天我在尝试类似的东西,当我使用 ajax.beginform 时遇到了同样的错误,所以使用了..

<form action="Home/upload" method="post" enctype="multipart/form-data">                       
 <input type="file" name="file" id="file" /><br />
 <input type="submit" value="Upload" />                       
</form>

在你看来试试这个,这可能会有所帮助..

于 2013-02-02T10:08:35.873 回答
0

您的控制器期待您的表单使用 post 的 httpget 位。如果您从控制器操作中删除 httpget 它应该可以工作。

于 2013-02-02T10:17:40.737 回答