2010-10-02 30 views
0

我是新來的asp.net mvc,但我已經徹底搜索了這個主題。作爲參數傳遞給方法時,ViewModel不受POST形式的約束

我命名PictureViewModel一個ViewModel類,這是一個視圖EditorTemplate從繼承的類: <%@控制語言= 「C#」 繼承= 「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都無濟於事。 我將不勝感激任何幫助! Andrew。

哦,是的,viewdata正確地填充文件名!

這不是工作,太 [HttpPost] 公衆的ActionResult創建(的FormCollection FC/[綁定(PREFIX = 「形式」)PictureViewModel下午 /) { 嘗試 { 變種P =新PictureViewModel (); UpdateModel(p,「form」,new [] {「Name」,「Description」}); ViewData [「name」] = Request.Files [0] .FileName; // TODO:在這裏添加插入邏輯 return View(「New」,p); return RedirectToAction(「Index」); } catch { return View(); } }

而這種代碼

 foreach (var k in fc) 
     { 
      Debug.WriteLine(k.ToString()); 
     } 

是生產

form.Name 
form.Description 
CategoryID 
Create 
+0

添加您的FormCollection,看看您收到的鑰匙。在裏面。 – Stefanvds 2010-10-02 13:29:50

+0

我試過這個,存在4個鍵。 – berezovskyi 2010-10-02 13:32:39

回答

1

在你的編輯模板,你應該使用強類型的輔助(TextBoxForTextAreaFor),你可以擺脫魔法串和前綴:

<div class="editor-label"><%: Html.LabelFor(m => m.Name) %></div> 
<div class="editor-field"><%: Html.TextBoxFor(m => m.Name) %></div> 

<div class="editor-label"><%: Html.LabelFor(m => m.Description) %></div> 
<div class="editor-field"><%: Html.TextAreaFor(m => m.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> 

要處理,你可以添加它們既可以作爲視圖模型屬性或動作參數上傳的文件:

public class PictureViewModel 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public HttpPostedFileBase ThumbFile { get; set; } 
    public HttpPostedFileBase FullFile { get; set; } 
} 

... 

[HttpPost] 
public ActionResult Create(PictureViewModel pm) 
{ 
    // Normally the pm variable should be fully 
    // initialized here from the POST request and you can use it directly 
    // No need to call UpdateModel 
    return View("New", pm); 
} 
相關問題