我在下面的代码中提交了问题。在 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; }
}