新しいユーザーを作成するビューがあります。このフォームには、新しいユーザーの名前と写真を含める必要があります。だから、それはこのように見えるはずです
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Project.Models.UserModel>" %>
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
title
</asp:Content>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.ValidationSummary("Error: Missing Values!") %>
<% using (Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%= Html.TextBoxFor(m => m.name)%>
<%= Html.TextBoxFor(m => m.uploadFile, new { type = "file" })%>
<input type="submit" value="Create User" />
<% } %>
</asp:Content>
私の UserModel は次のようになります。
public class UserModel
{
[Required]
public string name { get; set; }
[Required]
public HttpPostedFileBase uploadFile { get; set; }
}
私のコントローラーは次のようになります。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(UserModel model)
{
if (!ModelState.IsValid)
return View(model);
...
}
そのため、ユーザーが名前のみを入力すると、エラー メッセージが表示されます。また、入力した名前はまだテキストボックスにあります。私の問題は、ユーザーが画像のみを選択した場合(名前なし)、エラーが発生するが、ファイルブラウザで選択したファイルが保存されていないことです(そのため、再度選択する必要があります)。モデルを Create-View に戻すときに、HttpPostedFileBase uploadFileが<%= Html.TextBoxFor(m => m.uploadFile, new { type = "file" })%>と見なされる方法はありますか?