我有一个表单,用于上传具有指定名称和类别的文件。我有它设置所以使用ajax.beginform调用使用页面上的帖子。当我点击提交时,它不会做任何事情,只能重定向到该页面。
形式:
@using (Ajax.BeginForm("AttachFile", "Attachments", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "filesuccess", OnSuccess = "addFileSuccess", OnFailure = "addFileFailure" }, new { enctype = "multipart/form-data" }))
{
<div class="modal-body" id="addfilemodaltext">
<div class="form-group">
<div class="control-label">Select file to add to this collaboration:</div>
@Html.Hidden("CollaborationId", Model.Collaboration.Id)
<input type="file" name="FileContents" id="FileContents"/>
</div>
<div class="form-group">
<div class="control-label">Enter a description for this file: </div>
@Html.TextArea("FileDescription", null, new {@class = "form-control"})
</div>
<div class="form-group">
<div class="control-label">Select a Category for this file:</div>
@Html.DropDownList("categoryId", new SelectList(Model.AttachmentCategories, "Id", "Name"), new { @class = "form-control" })
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<input type="submit" class="btn btn-primary" id="addfilebtn" value="Upload File" />
</div>
}
控制器:
[HttpPost]
public ActionResult AttachFile(int CollaborationId, string FileDescription, int categoryId, HttpPostedFileBase FileContents)
{
if (FileContents != null && FileContents.ContentLength > 0)
{
var fileName = FileContents.FileName;
var mimeType = FileContents.ContentType;
var fileLength = FileContents.ContentLength;
byte[] fileData = new byte[fileLength];
FileContents.InputStream.Read(fileData, 0, fileLength);
var response = DchServiceUtility.ServiceClient.AddAttachmentToCollaboration(CollaborationId,
CurrentUser.Id, mimeType, FileDescription, fileName, fileData, categoryId, AppSettings.ClientName);
}
return RedirectToAction("Details", "Collaborations", new { @id = CollaborationId });
}
新的一点是它开始无法解决行动&#34; AttachFile&#34;。我做错了什么?
答案 0 :(得分:0)
我认为你需要像这样添加enctype属性:
@using (Ajax.BeginForm("YourAction", "YourController",
new AjaxOptions() { HttpMethod = "POST", //More options here },
new { enctype = "multipart/form-data"}))
修改:您的代码包含硬编码categoryId
和CollaborationId
@using (Ajax.BeginForm("AttachFile", "Home", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "filesuccess", OnSuccess = "addFileSuccess", OnFailure = "addFileFailure" }, new { enctype = "multipart/form-data" }))
{
<div class="modal-body" id="addfilemodaltext">
<div class="form-group">
<div class="control-label">Select file to add to this collaboration:</div>
@Html.Hidden("CollaborationId", 1)
<input type="file" name="FileContents" id="FileContents" />
</div>
<div class="form-group">
<div class="control-label">Enter a description for this file: </div>
@Html.TextArea("FileDescription", null, new { @class = "form-control" })
</div>
<div class="form-group">
<div class="control-label">Select a Category for this file:</div>
@Html.Hidden("categoryId", 1)
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<input type="submit" class="btn btn-primary" id="addfilebtn" value="Upload File" />
</div>
}
<强>结果:强>