I have submit problem with code below. On Firebug I get this message:
我在下面的代码中提交了问题。在Firebug上,我收到此消息:
POST localhost:9706/Home/Upload 500 Internal Server Error
POST localhost:9706 / Home / Upload 500内部服务器错误
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.
描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
异常详细信息:System.MissingMethodException:没有为此对象定义的无参数构造函数。
This form I can submit but without jquery and unobtrusive-ajax scripts and in that case submit looks like normal submit (whole page submit, without ajax)
这个表单我可以提交,但没有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>
Controller code
[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");
}
Model code
public class FileInfoModel
{
public FileInfoModel(){}
public string Name { get; set; }
public double Length { get; set; }
public DateTime LastWriteTime { get; set; }
}
2 个解决方案
#1
0
I was trying something similar yesterday and i got the same error when i used ajax.beginform so insted of that use ..
我昨天尝试了类似的东西,当我使用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>
try this in ur view side this may help..
在你看来这个尝试这可能有帮助..
#2
0
Your controller is expecting a httpget bit your form is using post. If you remove the httpget from the controller action it should work.
你的控制器期待你的表单正在使用post的httpget位。如果从控制器操作中删除httpget,它应该可以工作。
#1
0
I was trying something similar yesterday and i got the same error when i used ajax.beginform so insted of that use ..
我昨天尝试了类似的东西,当我使用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>
try this in ur view side this may help..
在你看来这个尝试这可能有帮助..
#2
0
Your controller is expecting a httpget bit your form is using post. If you remove the httpget from the controller action it should work.
你的控制器期待你的表单正在使用post的httpget位。如果从控制器操作中删除httpget,它应该可以工作。