我正在创建一个简单的表单来上传文件(可以给出一个友好的名字)。
我已经一次又一次地查看了这段代码,但是当表单发布到/ MyEntities / Add时这总是得到404(这是一个仅限帖子的URL)。
任何想法都会非常感激 - 我根本看不出我做错了什么。
控制器:
public class MyEntitiesController : Controller
{
private DataFilesComparisonRepository repository = new DataFilesComparisonRepository();
public ActionResult Index()
{
List<MyEntitiesDataset> datasets = repository.GetMyEntitiesDatasets();
return View(datasets);
}
[HttpPost]
public ActionResult Add(HttpPostedFileBase postedFile, string friendlyName)
{
repository.AddMyEntities(friendlyName, postedFile.FileName, postedFile.InputStream);
return RedirectToAction("Index");
}
}
观点:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<MyEntitiesDataset>>" %>
<%@ Import Namespace="DataFilesComparison.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
The Title
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.BeginForm("Add", "MyEntities", FormMethod.Post, new { enctype = "multipart/form-data" }); %>
<p>
<input type="file" name="postedFile" />
<label for="friendlyName">Friendly Name:</label>
<%= Html.TextBox("friendlyName") %>
<input type="submit" value="Add" />
</p>
<% Html.EndForm(); %>
<% if (Model == null || Model.Count == 0) { %>
<p>No datasets found.</p>
<% } else { %>
<ul>
<% foreach (MyEntitiesDataset dataset in Model) { %>
<li>
<%= dataset.Name %>
[<%= Html.ActionLink("X", "Delete", new { ID = dataset.ID })%>]
</li>
<% } %>
</ul>
<% } %>
</asp:Content>
答案 0 :(得分:0)
您传递到视图中的模型与您在Add()方法中发布的模型不同,这可能会使默认路由设置混乱。虽然我看到你明确指出要调用哪种方法,但我之前遇到过这样的问题。也许尝试使用GET和POST所需的属性创建一个ViewModel,然后在View和Controller方法中使用它们。像这样:
<强>模型强>
public MyViewModel
{
public List<MyEntitiesDataset> Datasets {get; set;}
public HttpPostedFileBase PostedFile {get; set;}
public string FriendlyName {get; set;}
}
控制器方法
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
model.Datasets = repository.GetMyEntitiesDatasets();
return View(datasets);
}
[HttpPost]
public ActionResult Add(MyViewModel model)
{
repository.AddMyEntities(model.FriendlyName, Model.PostedFile.FileName, Model.postedFile.InputStream);
return RedirectToAction("Index", model);
}
查看强>
%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>
<%@ Import Namespace="DataFilesComparison.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
The Title
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.BeginForm("Add", "MyEntities", FormMethod.Post, new { enctype = "multipart/form-data" }); %>
<p>
<input type="file" name="postedFile" />
<label for="Model.FriendlyName">Friendly Name:</label>
<%= Html.TextBoxFor(m => m.FriendlyName) %>
<input type="submit" value="Add" />
</p>
<% Html.EndForm(); %>
<% if (Model.Datasets == null || Model.Datasets.Count == 0) { %>
<p>No datasets found.</p>
<% } else { %>
<ul>
<% foreach (var dataset in Model.Datasets) { %>
<li>
<%= dataset.Name %>
[<%= Html.ActionLink("X", "Delete", new { ID = dataset.ID })%>]
</li>
<% } %>
</ul>
<% } %>
</asp:Content>
你可能不得不玩文件上传器(不确定是否有Html.FileFor,但希望你明白了!
祝你好运。