2012-03-17 20 views
1

我正在製作一個具有下拉菜單的視圖。該觀點與CategoryModel看起來像這樣提供:返回到控制器中的操作時,已過帳的DDL列表爲空

public class CategoryModel 
{ 
    [Required] 
    [Display(Name = "Categories")] 
    public List<Category> Categories { get; set; } 

    [Required] 
    [GreaterThan(ExceedValue = 0, ErrorMessage = "Please select a category.")] 
    [Display(Name = "SelectedCategoryId")] 
    public int SelectedCategoryId { get; set; } 
} 

分類的列表視圖中使用,採取先分類來填充下拉列表並把它們放到一個的SelectList,像這樣:

@model RatingMVC3.Models.CategoryModel 

@{ 
Layout = "~/Views/Shared/_MainLayout.cshtml"; 
ViewBag.Title = "Upload"; 
} 

<h2>Upload</h2> 

@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="file" /> 
    <input type="submit" value="OK" /> 
    @Html.DropDownListFor(m => m.SelectedCategoryId, new SelectList(Model.Categories, "Id", "Description"), "-- Select Category --"); 
    @Html.ValidationMessageFor(m => m.SelectedCategoryId); 
    @Html.HiddenFor(m => m.Categories); 
} 

當提交此表單並將模型返回給Controller時,我可以看到該模型返回到Controller中,但它包含一個空List,而不是View中存在的Categories列表。 (SelectedCategoryId如預期的那樣)。這是在控制器中的ActionResult方法:

[HttpPost] 
    [Authorize] 
    public ActionResult Upload(HttpPostedFileBase file, CategoryModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      if (file != null && file.ContentLength > 0) 
      { 
       var FileExtension = Path.GetExtension(file.FileName); 
       string Path1 = null; 
       string FileName = null; 
       do 
       { 
        var randomName = Path.GetRandomFileName(); 
        FileName = Path.ChangeExtension(randomName, FileExtension); 
        Path1 = Path.Combine(Server.MapPath("~/Images"), FileName); 
       } while (System.IO.File.Exists(Path1)); 
       file.SaveAs(Path1); 
       if (UploadService.SaveImage(FileName, System.Web.HttpContext.Current.User.Identity.Name, model.SelectedCategoryId)) 
       { 
        return RedirectToAction("Uploaded", "Upload"); 
       } 
      } 
      return RedirectToAction("Index", "Home"); 
     } 

     return View(model); 
    } 

空表對我來說是一個問題,因爲你可以看到,如果ModelState中是無效的,視圖將被用相同型號又回來了,需要填充類別。 我希望有人能回答這個;)我會很樂意指定更多的信息,如果需要的話,在此先感謝

+0

是否在所有類別的GET之後填充下拉菜單? – 2012-03-17 14:50:59

+0

類別在GET操作方法中獲取並放入模型中,並且隨着View在GET方法中返回,模型(包含列表)將作爲參數傳遞給View,其中Dropdown是填充。 [Authorize] public ActionResult上傳() { CategoryModel CategoryModel1 = UploadService.GetCategories(); return View(CategoryModel1); } – emin 2012-03-17 17:21:02

回答

0

在POST操作期間,整個選擇列表(下拉列表)不會返回到控制器/表單操作,只有從該列表中選擇的值。如果由於驗證不正確而必須重新渲染視圖,則可以a)由於驗證原因進行AJAX提交而不是完整的POST,或者b)重新創建列表並將其發送回錯誤視圖。選項B)可以實現爲這樣:

[HttpPost] 
    [Authorize] 
    public ActionResult Upload(HttpPostedFileBase file, CategoryModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      if (file != null && file.ContentLength > 0) 
      { 
       var FileExtension = Path.GetExtension(file.FileName); 
       string Path1 = null; 
       string FileName = null; 
       do 
       { 
        var randomName = Path.GetRandomFileName(); 
        FileName = Path.ChangeExtension(randomName, FileExtension); 
        Path1 = Path.Combine(Server.MapPath("~/Images"), FileName); 
       } while (System.IO.File.Exists(Path1)); 
       file.SaveAs(Path1); 
       if (UploadService.SaveImage(FileName, System.Web.HttpContext.Current.User.Identity.Name, model.SelectedCategoryId)) 
       { 
        return RedirectToAction("Uploaded", "Upload"); 
       } 
      } 
      return RedirectToAction("Index", "Home"); 
     } 
     //I don't know how you fetch this list, but just do it again before returning 
     //the view 
     model.Categories = GetMyCategories(); 
     return View(model); 
    } 

我想如果你願意,你可以存放物品,您的網頁上隱藏的輸入列表中,但只是長相醜陋,增加的數據量是發送到網頁和從網頁發出,天哪就是它,只是聽起來像ViewState(榨汁機)很多。

+0

謝謝你的回答。我覺得像一個AJAX調用似乎有點像過度它應該是一個更簡單的東西。我覺得像在控制器中再次獲取類別以將其返回到無效後的視圖似乎增加了對數據庫的查詢量,這是我試圖避免的。我不確定更糟糕的是,更多的數據從客戶端發送到Web服務器或服務器上額外的數據庫查詢?但是,如果沒有其他人有這個魔術解決方案,我會抓住你的替代選擇。再次感謝 – emin 2012-03-17 17:49:30

+0

你必須記住兩件事。 1:你只在一個不好的模型狀態下執行這個操作,這個狀態應該只是這個方法返回的一小部分,並且2:過早優化可能會導致很多浪費時間的很少的改進(在本例中爲一個SQL查詢,也許可能20毫秒):)讓我們知道你決定做什麼。 – Tommy 2012-03-17 21:32:47

相關問題