我是 asp.net mvc 的新手,但我到处都在这个主题上进行了彻底的搜索。
我有一个名为 PictureViewModel 的 ViewModel 类,它是 EditorTemplate 视图继承自的类:<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div class="editor-label"><%: Html.LabelFor(m=>m.Name) %></div>
<div class="editor-field"><%: Html.TextBox("form.Name") %></div>
<div class="editor-label"><%: Html.LabelFor(m=>m.Description) %></div>
<div class="editor-field"><%: Html.TextArea("form.Description") %></div>
<% Html.RenderAction("SelectCategory", "Category"); %>
<div class="editor-label"><label for="thumbFile">Thumb</label></div>
<div class="editor-field"><input id="thumbFile" name="thumbFile" type="file"/></div>
<div class="editor-label"><label for="fullFile">Full</label></div>
<div class="editor-field"><input id="fullFile" name="fullFile" type="file"/></div>
我也有文件字段和所有发布到自己的
<%@ Page Title="" Language="C#" MasterPageFile="/Views/Shared/Site_Table.Master"
Inherits="System.Web.Mvc.ViewPage<MvcApplicationVelarPolya.Areas.Admin.Models.PictureViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<%
using (Html.BeginForm("create", "Pictures", FormMethod.Post, new {enctype="multipart/form-data"}))
{%>
<%: Html.EditorForModel() %>
<input type="submit" name="Create" />
<% }%>
</asp:Content>
这是专门用于处理提交的代码:
[HttpPost]
public ActionResult Create([Bind(Prefix="form")] PictureViewModel pm)
{
try
{
var p = new PictureViewModel();
UpdateModel(p, "",new[] {"Name", "Description"});
ViewData["name"] = Request.Files[0].FileName;
return View("New", p);
}
catch
{
return View();
}
}
添加前缀或将 FormCollection 与 UpdateModel 一起使用都无济于事。我将不胜感激任何帮助!安德鲁。
哦,是的,viewdata 正确地填充了文件名!
这也不起作用 [HttpPost] public ActionResult Create(FormCollection fc/ [Bind(Prefix="form")] PictureViewModel pm /) { try { var p = new PictureViewModel(); UpdateModel(p, "form",new[] {"Name", "Description"}); ViewData["name"] = Request.Files[0].FileName; // TODO: 在此处添加插入逻辑 return View("New", p); return RedirectToAction("索引"); } 捕捉 { 返回视图();} }
而这段代码
foreach (var k in fc)
{
Debug.WriteLine(k.ToString());
}
正在生产
form.Name
form.Description
CategoryID
Create