2010-09-24 54 views
1

我在控制器和視圖中獲得以下代碼。問題是模型(Photo是一個實體框架實體)是空的(所有字段都是空的)。爲什麼?提交asp.net mvc 2表格時的空模型

// GET: /Admin/Photo/Create 

    public ActionResult Create() 
    { 
     return View(); 
    } 

    // 
    // POST: /Admin/Photo/Create 

    [HttpPost] 
    public ActionResult Create(int id, FormCollection collection) 
    { 
     try 
     { 
      var file = (HttpPostedFileBase) Request.Files[0]; 
      if (file != null && file.FileName != null) 
      { 
       var filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Photos/Product/", Path.GetFileName(file.FileName)); 
       file.SaveAs(filename); 
       var photo = new Photo(); 
       photo.Description = collection["Description"]; 
       photo.Main = collection["Main"].Contains("true"); 
       photo.Filename = Path.GetFileName(file.FileName); 
       photo.Product_Id = id; 
       Entities.AddToPhotos(photo); 
       Entities.SaveChanges(); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Plik musi zostać załadowany."); 
       return View(); 
      } 


      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 

<h2>Create</h2> 

<% using (Html.BeginForm(null, null, null, FormMethod.Post, new {enctype = "multipart/form-data" })) {%> 
    <%: Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Fields</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Description) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.Description) %> 
      <%: Html.ValidationMessageFor(model => model.Description) %> 
     </div> 

     <div class="editor-label"> 
      <label for="MainContent_file">Plik: </label> 
     </div> 
     <div class="editor-field"> 
      <asp:FileUpload ID="file" runat="server" /> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Main) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.CheckBoxFor(model => model.Main) %> 
      <%: Html.ValidationMessageFor(model => model.Main) %> 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 

<% } %> 

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

更新:我檢查和收集填充了適當的領域,但它們都清零。

+0

只是一個單純的拷貝,並與您的代碼和HTML粘貼 - 用只包含描述及主要的模型 - 它工作得很好。該問題可能存在於此特定文件之外。您正在使用asp:FileUpload控件。嘗試用輸入替換它。但是,您必須刪除主頁面中的

標籤。 – Buildstarted 2010-09-24 17:23:19

+0

沒有FileUpload控件,它工作得很好。我不必刪除masterpage中的標籤 - 它是Asp.net MVC,我在主頁面中沒有。 – Agares 2010-09-24 17:35:22

+0

嗯,我問的表單標籤的原因是因爲一個新的項目與asp:FileUpload標籤拋出一個錯誤,它需要與runat =服務器的表單標籤...所以基於該表單標籤肯定存在,在某處。 – Buildstarted 2010-09-24 19:26:14

回答

1

檢查以確保生成的html中的名稱屬性與您的集合名稱匹配。您也可以將public ActionResult Create(int id, FormCollection collection)更改爲public ActionResult Create(int id, YourViewModel model),以自動將帖子值映射到模型。

+0

我確信表單中的名稱與模型中的名稱相同。我已將FormCollection更改爲Photo,但它無濟於事。 – Agares 2010-09-24 15:30:22

+1

您是否設置了斷點並驗證集合是否具有表單文章中的相應值? – Buildstarted 2010-09-24 15:48:46

+0

+1請參閱我的答案,如果正如我所描述的那樣,自動映射不會發生此問題。也就是說,OP的模型似乎不是視圖模型,所以最好不要使用自動綁定(我傾向於將視圖模型引入到我想要的字段中)。 – eglasius 2010-09-24 15:54:28

0

檢查瀏覽器中的html源代碼。

這可能給他們爲:「辛普森」

+0

- 我認爲沒關係。 – Agares 2010-09-24 16:31:17

+0

@Agares沒有想到,我建議用調試器檢查它作爲BuildStarted建議,以查看該集合是否正在填充任何內容。至於爲什麼會發生這種情況,我不確定/在問題的更新中發佈你的發現,有人可能會給你答案的額外信息。 – eglasius 2010-09-24 16:36:16