在嵌入式用户控件 .ascx 文件上的 MVC 中进行文件上传
按照此处的示例:
我有一个 .ascx 文件用作编辑器模板,我想使用此方法上传文件,我的 .ascx 文件看起来像这样
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MHNHub.Models.ScrollerLink>" %>
<script type="text/javascript">
$(function () {
$("#dialog").dialog({
bgiframe: true,
height: 160,
width: 400,
modal: true,
autoOpen: false,
resizable: true
})
});
</script>
<div class="editor-label">
Content Title
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ContentTitle) %>
<%: Html.ValidationMessageFor(model => model.ContentTitle) %>
</div>
<div class="editor-label">
Content Description
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ContentDescription) %>
<%: Html.ValidationMessageFor(model => model.ContentDescription)%>
</div>
<div class="editor-label">
Current Image
</div>
<img src="<%: Model.ImageUrl %>" />
<div class="editor-field">
<div id="dialog" title="Upload Image">
<% using (Html.BeginForm("Upload", "HomeScroller", FormMethod.Post, new { enctype="multipart/form-data" }))
{ %>
Select a file: <input type="file" name="fileUpload" />
<input type="submit" value="Upload" />
<% } %>
</div>
<a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload Image</a>
</div>
<div class="editor-label">
URL Link
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ImageLink) %>
<%: Html.ValidationMessageFor(model => model.ImageLink) %>
</div>
我的控制器称为 HomeScrollerController ,操作方法称为 Upload,我的控制器和相关操作如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MHNHub.Models;
using MHNHub.ViewModels;
using System.Text.RegularExpressions;
using System.IO;
namespace MHNHub.Controllers
{
public class HomeScrollerController : Controller
{
//
// GET: /HomeScroller/Create
public ActionResult Create()
{
var scrollerLink = new ScrollerLink();
var viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink
};
return View(viewModel);
}
private ScrollerLink GenerateScrollerLinkImageID(ScrollerLink scrollerLink)
{
var scrollerLinkList = dataContext.ScrollerLinks.ToList();
int count = 1;
foreach (var s in scrollerLinkList)
{
//Loop through the scroller link items and fix their ImageID, to remove any gaps if anything has been deleted
s.ImageID = "slide-img-" + count;
count++;
}
//Set this scrollerLinks ImageID to the last in the list.
scrollerLink.ImageID = "slide-img-" + count;
return scrollerLink;
}
public ActionResult Upload()
{
HttpPostedFileBase file = Request.Files["OriginalLocation"];
HomeScrollerViewModel viewModel;
Regex imageFilenameRegex = new Regex(@"(.*?)\.(jpg|jpeg|png|gif)$");
ScrollerLink scrollerLink = new ScrollerLink();
if (file.ContentLength > 0)
{
if (!imageFilenameRegex.IsMatch(file.FileName))
{
viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink,
HasError = true,
ErrorMessage = "Image must be .jpg, .jpeg, .png, or .gif"
};
}
else
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
file.SaveAs(filePath);
viewModel = new HomeScrollerViewModel()
{
ScrollerLink = new ScrollerLink()
{
SlideID = scrollerLink.SlideID,
ImageID = scrollerLink.ImageID,
ImageUrl = filePath,
ImageAlt = scrollerLink.ImageAlt,
ContentTitle = scrollerLink.ContentTitle,
ContentDescription = scrollerLink.ContentDescription,
ImageLink = scrollerLink.ImageLink
}
};
}
}
else
{
viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink,
HasError = true,
ErrorMessage = "Image is empty!? Try Again"
};
}
return View(viewModel);
}
//
// POST: /HomeScroller/Create
[HttpPost]
public ActionResult Create(ScrollerLink scrollerLink)
{
try
{
scrollerLink = GenerateScrollerLinkImageID(scrollerLink);
dataContext.ScrollerLinks.InsertOnSubmit(scrollerLink);
dataContext.SubmitChanges();
return RedirectToAction("Index");
}
catch(Exception ex)
{
var viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink,
HasError = true,
ErrorMessage = ex.Message
};
return View(viewModel);
}
}
}
}
我遇到的问题是,单击表单上的提交按钮时,不会调用我的操作方法。我已经在一个新项目中尝试过这个示例(但不是在 .ascx 上)并且它有效。我在这方面花费了太多时间,需要继续前进,我是 MVC 新手,具有 Web 窗体背景。感谢您的帮助!有什么想法堆栈溢出吗?
Following the example here:
I have an .ascx file I'm using as an editor template, and I would like to use this method to upload files, my .ascx file looks like so
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MHNHub.Models.ScrollerLink>" %>
<script type="text/javascript">
$(function () {
$("#dialog").dialog({
bgiframe: true,
height: 160,
width: 400,
modal: true,
autoOpen: false,
resizable: true
})
});
</script>
<div class="editor-label">
Content Title
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ContentTitle) %>
<%: Html.ValidationMessageFor(model => model.ContentTitle) %>
</div>
<div class="editor-label">
Content Description
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ContentDescription) %>
<%: Html.ValidationMessageFor(model => model.ContentDescription)%>
</div>
<div class="editor-label">
Current Image
</div>
<img src="<%: Model.ImageUrl %>" />
<div class="editor-field">
<div id="dialog" title="Upload Image">
<% using (Html.BeginForm("Upload", "HomeScroller", FormMethod.Post, new { enctype="multipart/form-data" }))
{ %>
Select a file: <input type="file" name="fileUpload" />
<input type="submit" value="Upload" />
<% } %>
</div>
<a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload Image</a>
</div>
<div class="editor-label">
URL Link
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ImageLink) %>
<%: Html.ValidationMessageFor(model => model.ImageLink) %>
</div>
My controller is called HomeScrollerController and the action method is called Upload, my controller and relevent actions are shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MHNHub.Models;
using MHNHub.ViewModels;
using System.Text.RegularExpressions;
using System.IO;
namespace MHNHub.Controllers
{
public class HomeScrollerController : Controller
{
//
// GET: /HomeScroller/Create
public ActionResult Create()
{
var scrollerLink = new ScrollerLink();
var viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink
};
return View(viewModel);
}
private ScrollerLink GenerateScrollerLinkImageID(ScrollerLink scrollerLink)
{
var scrollerLinkList = dataContext.ScrollerLinks.ToList();
int count = 1;
foreach (var s in scrollerLinkList)
{
//Loop through the scroller link items and fix their ImageID, to remove any gaps if anything has been deleted
s.ImageID = "slide-img-" + count;
count++;
}
//Set this scrollerLinks ImageID to the last in the list.
scrollerLink.ImageID = "slide-img-" + count;
return scrollerLink;
}
public ActionResult Upload()
{
HttpPostedFileBase file = Request.Files["OriginalLocation"];
HomeScrollerViewModel viewModel;
Regex imageFilenameRegex = new Regex(@"(.*?)\.(jpg|jpeg|png|gif)$");
ScrollerLink scrollerLink = new ScrollerLink();
if (file.ContentLength > 0)
{
if (!imageFilenameRegex.IsMatch(file.FileName))
{
viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink,
HasError = true,
ErrorMessage = "Image must be .jpg, .jpeg, .png, or .gif"
};
}
else
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
file.SaveAs(filePath);
viewModel = new HomeScrollerViewModel()
{
ScrollerLink = new ScrollerLink()
{
SlideID = scrollerLink.SlideID,
ImageID = scrollerLink.ImageID,
ImageUrl = filePath,
ImageAlt = scrollerLink.ImageAlt,
ContentTitle = scrollerLink.ContentTitle,
ContentDescription = scrollerLink.ContentDescription,
ImageLink = scrollerLink.ImageLink
}
};
}
}
else
{
viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink,
HasError = true,
ErrorMessage = "Image is empty!? Try Again"
};
}
return View(viewModel);
}
//
// POST: /HomeScroller/Create
[HttpPost]
public ActionResult Create(ScrollerLink scrollerLink)
{
try
{
scrollerLink = GenerateScrollerLinkImageID(scrollerLink);
dataContext.ScrollerLinks.InsertOnSubmit(scrollerLink);
dataContext.SubmitChanges();
return RedirectToAction("Index");
}
catch(Exception ex)
{
var viewModel = new HomeScrollerViewModel()
{
ScrollerLink = scrollerLink,
HasError = true,
ErrorMessage = ex.Message
};
return View(viewModel);
}
}
}
}
The issue that I am having is that my action method Isn't being called when the submit button on the form is clicked. I have tried this example from a new project(but not on an .ascx) and it works. I've spent far too much time on this and need to move forward, I'm new to MVC, with a Web Forms background. Your help is appreciated! Any Ideas Stack Overflow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是嵌套表单。用户控件及其表单嵌套在我的 .aspx 页面上的另一个表单内。该动作现在可以正确触发。
The issue was a nested form. The user control and its form was nested inside of another form on my .aspx page. The action fires correctly now.