2015-03-19 35 views
0

我使用簡單的MVC應用程序。在我的EditProduct視圖中,我想在禁用輸入中顯示產品的當前類別。我有一個下拉列表,用戶可以選擇帶選項標籤「選擇類別」的類別。如何在ModelState.IsValid返回false後顯示值

查看EditProduct

<div class="form-group"> 
      <label for="disabledInput" class="control-label col-md-2"><b>Current Category</b></label> 
      <div class="col-md-10"> 
       <input class="form-control" id="disabledInput" type="text" placeholder="@Model.Category" disabled> 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.CategoryID, "Category", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("CategoryID", (SelectList)ViewBag.CategoryID, "Select Category", htmlAttributes: new { @class = "form-control " }) 
       @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-success" }) 
      </div> 
     </div> 

我把輸入值@Model.Category的佔位符。這是當前產品的名稱。

用戶需要選擇類別:

public class ProductsViewModel 
{ 
    \\ ..... 

    public string Category { get; set; } 

    [Required(ErrorMessage = "The Category is required.")] 
    public int CategoryID { get; set; } 
} 

當我選擇的產品編輯,一切都OK了 - 當前類是在輸入顯示。如果我選擇類別並點擊保存也可以。

但是,如果我不選擇類別並單擊保存,ModelState.IsValid返回false,然後@Model.Category已從輸入中消失。

如何使用此方法在ModelState.IsValid返回false後顯示產品當前類別?或者另一種方式來做到這一點?

+0

您的代碼存在一些潛在的問題。首先,被禁用的輸入不會回發任何內容,因此當您返回視圖時,它將始終爲空(而不是隻讀)。你不應該把'SelectList'命名爲你綁定的屬性的名字(把它改成'ViewBag.CategoryList') – 2015-03-19 02:22:32

+0

你的禁用輸入也沒有'name'或'value'屬性,所以它不會即使您只是只讀,也要將任何內容發回。創建一個新的屬性說CurrentCategory並綁定到它。 – 2015-03-19 02:24:59

+0

@StephenMuecke我將viewbag的名稱更改爲'ViewBag.CategoryList',這將我當前的產品類別綁定到下拉列表。我想那樣。我刪除輸入並且現在可以繼續使用。感謝幫助! – dondomates 2015-03-19 02:40:09

回答

1

您手動創建的輸入沒有name屬性或value屬性,並且是disabled。所有3意味着沒有任何關於該輸入的內容將回傳,因此當您返回視圖時,Categorynull。下一個問題是您的下拉列表也綁定到名爲CategoryID的屬性,並且您的SelectList也具有相同的名稱,這將導致問題。那麼你有一個類,它表明它的視圖模型,但繼續使用ViewBag(爲什麼?)。

視圖模型更改爲

public class ProductsViewModel 
{ 
    ..... 

    public string Category { get; set; } // see notes below 

    [Display(Name = "Category")] 
    [Required(ErrorMessage = "The Category is required.")] 
    public int CategoryID { get; set; } 
    public SelectList CategoryList { get; set; } 
} 

然後在視圖

@Html.LabelFor(model => model.CategoryID, new { @class = "control-label col-md-2" }) 
@Html.DropDownListFor(m => m.CategoryID, Model.CategoryList, "Select Category", new { @class = "form-control " }) 
@Html.ValidationMessageFor(model => model.CategoryID, new { @class = "text-success" }) 

和Controller現在

public ActionResult Edit(int ID) 
{ 
    ProductsViewModel model = new ProductsViewModel(); 
    model.CategoryList = new SelectList(db.Categories, "ID", "Name"); // adjust parameters to suit 
    model.CategoryID = ?; // set this if you want an initial option displayed 
    return View(model); 
} 

當頁面最初呈現時,選中的選項基於CategoryID的值顯示。它不清楚爲什麼你也想顯示一個文本框的值相同爲好,但如果是這樣,則該類別名稱分配財產string Category和使用

@Html.TextBoxFor(m => m.Category, new { readonly = "readonly" }) 

,這將創造一個「只讀」文本框包含初始值Category並將回傳。

+0

你的解釋對我很有幫助。我糾正了我的代碼,現在很清晰,並且工作完美。這是我第一個項目,我知道要學習很多東西,但是您對視圖模型的概念有很大的幫助。對不起,我的英語,謝謝! – dondomates 2015-03-19 03:46:38

相關問題