无法提交ajax表单内部错误500

时间:2013-02-02 09:22:53

标签: jquery ajax asp.net-mvc asp.net-mvc-4

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

POST localhost:9706 / Home / Upload 500 Internal Server Error

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

描述:执行当前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; }       
}

2 个答案:

答案 0 :(得分: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>

在你的观点方面试试这可能会有所帮助..

答案 1 :(得分:0)

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