按照这里的例子:
我有一个.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窗体背景。非常感谢您的帮助!任何想法堆栈溢出?
答案 0 :(得分:0)
问题是嵌套表格。用户控件及其表单嵌套在我的.aspx页面上的另一个表单中。该动作现在正确启动。