2014-05-13 33 views
0

並不反映我有一個模型,看起來像:Html.DropDownListFor&Html.CheckBoxFor對系統

public class schoolModel 
{ 
    public SelectList Categories; 
    public school Entity; 


} 

哪裏上學的樣子:

public class school 
{ 

int? CategoryId{get;set;} // foreingKey 
bool IncludeAttached{get;set;} 

} 

我初始化schoolModel如下:

var model = new schoolModel(); 
      model.Entity = schoolServiceAgent.Output.Entities.FirstOrDefault(n => n.Id == id); 
      model.Categories = new SelectList(new GenderServiceAgent().GetCategoriesByAlias(null, true), "Id", "Alias", model.Entity.CategoryId); 

我用他們喜歡

@Html.DropDownListFor(model => model.Entity.CategoryId, new SelectList(Model.Categories, "Value", "Text"), Model.Entity.CategoryId) 
@Html.CheckBoxFor(model => model.Entity.IncludeAttached.Value, new { @class = "form-control placeholder-no-fix", name = "IncludeAttached", @checked = "checked" }) 

,但在編輯崗位的FormCollection沒有價值的CategoryId,並IncludeAttached

我試圖讓他們使用

public ActionResult Edit(int id, FormCollection collection) 
{ 
    var categoryid = int.Parse(collection["CategoryId"]); 
    var result = selectedschool.inMediaArabicApp = bool.Parse(collection["IncludeAttached"]); 

} 

上述加薪空例外,任何想法如何解決呢?周圍的編輯形式看起來像

@using (@Html.BeginForm("Edit", "Schools", FormMethod.Post, 
          new { enctype = "multipart/form-data" })) 
{ 
+0

你可以添加你的HTML'form'代碼嗎? –

+0

我更新了問題 – AMH

+0

如果您使用'POST',那麼您需要在控制器操作[HttpPost]上添加一個註釋' –

回答

3

而不是使用的FormCollection你爲什麼不使用.NET Model binding能力?

使用[HttpPost],因爲您從表單發佈。

[HttpPost] 
public ActionResult Edit(School model) 
{ 
    var categoryid = model.CategoryId; 
    var result = model.IncludeAttached; 

} 

此外,c#約定將使用Pascal Casing作爲類名和變量。

+0

我試過但模型爲空 – AMH

+0

您是否安裝了Fiddler?你能告訴你的表單發送回服務器嗎? –