私はasp.netmvcを初めて使用しますが、このトピックについてはどこでも徹底的に検索しました。
EditorTemplateビューが継承するクラスであるPictureViewModelという名前のViewModelクラスがあります:<%@ 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();
}
}
プレフィックスを追加したり、UpdateModelでFormCollectionを使用したりしても役に立ちません。助けていただければ幸いです。アンドリュー。
そうそう、viewdataにはファイル名が正しく入力されています!
これも機能していません[HttpPost]publicActionResult Create(FormCollection fc / [Bind(Prefix = "form")] PictureViewModel pm /){try {var p = new PictureViewModel(); UpdateModel(p、 "form"、new [] {"名前"、 "説明"}); ViewData ["name"] = Request.Files [0] .FileName; // TODO:ここに挿入ロジックを追加return View( "New"、p); RedirectToAction( "Index");を返します。} catch {return View(); }}
そしてこのコード
foreach (var k in fc)
{
Debug.WriteLine(k.ToString());
}
生産しています
form.Name
form.Description
CategoryID
Create