C# 未在asp.net mvc中发布Ajax表单
我有一个表单,可以上载具有指定名称和类别的文件。我已经设置好了,所以使用ajax.beginform调用在页面上发布文章。当我点击submit时,它除了重定向到页面外什么都不做 表格:C# 未在asp.net mvc中发布Ajax表单,c#,jquery,asp.net,ajax,asp.net-mvc,C#,Jquery,Asp.net,Ajax,Asp.net Mvc,我有一个表单,可以上载具有指定名称和类别的文件。我已经设置好了,所以使用ajax.beginform调用在页面上发布文章。当我点击submit时,它除了重定向到页面外什么都不做 表格: 新的一点是,它开始无法解析action AttachFile。我做错了什么?我认为您需要添加如下enctype属性: @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOptions() {
新的一点是,它开始无法解析action AttachFile。我做错了什么?我认为您需要添加如下enctype属性:
@using (Ajax.BeginForm("YourAction", "YourController",
new AjaxOptions() { HttpMethod = "POST", //More options here },
new { enctype = "multipart/form-data"}))
编辑:使用硬编码的categoryId和CollaborationId编辑代码
结果:
重定向到哪个页面?这个视图放在哪里?是否引用了jquery.unobtrusive-ajax.js?是的,视图放置的位置。是的,我有js Referenced此视图是否与“AttachFile”操作从同一控制器返回?返回的视图不在同一控制器中。可能控制器操作上方缺少[HttpPost]属性我已更新代码以包含我当前拥有的内容我已测试过您的代码,效果良好。我所做的唯一更改是硬编码CollaborationId和categoryId
[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 });
}
@using (Ajax.BeginForm("YourAction", "YourController",
new AjaxOptions() { HttpMethod = "POST", //More options here },
new { enctype = "multipart/form-data"}))
@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>
}