2014-09-28 26 views
0

我有一個從像下面綁定參數與ASP.NET MVC GET方法5

<form action="/" method="get"> 
    <input type="radio" name="sort" value="date-desc" checked> 
    <input type="radio" name="sort" value="price-desc"> 
    <input type="radio" name="sort" value="price-asc"> 
    <input type="radio" name="sort" value="like-desc"> 
    <input type="radio" name="sort" value="like-asc"> 
    <input type="checkbox" name="show-discount"> 
    <input type="checkbox" name="show-new"> 
    <input type="submit" class="btn btn-default" value="Search"> 
</form> 

它返回URL等:<site>?sort=date-desc&show-discount=on&show-new=on

我想其綁定到控制器動作,動作象下面這樣:

public ActionResult Index([Bind(Prefix = "sort")]string Sort, 
    [Bind(Prefix = "show-discount")]bool? ShowDiscount = null, 
    [Bind(Prefix = "show-new")]bool? ShowNew = null) 
{ 

} 

問題是ShowDiscountShowNew參數總是空(沒有正確綁定)。

我認爲這個複選框造成的問題檢查時寫爲cb-name=on而不是cb-name=true。並且當複選框未被選中時,它不會寫在url上。

有沒有適當的方法來做到這一點?

有沒有辦法將Sort參數映射到枚舉?

+0

使用視圖模型和強類型化html helper,並讓MVC爲您完成所有工作。 – 2014-09-28 23:27:24

回答

1

使用視圖模型

型號

public class MyModel 
{ 
    public string sort { get; set; } 
    public bool show-discount { get; set; } 
    public bool show-discount { get; set; } // or bool? if you want it to be nullable 
    public bool show-new { get; set; } 
} 

控制器

public ActionResult Edit() 
{ 
    MyModel model = new MyModel(); 
    model.date-desc = "date-desc"; // set default 
    return View(model) 
} 

public ActionResult Edit(MyModel model) 
{ 
    .... // model is correctly bound with selected values 

查看

@model MyModel 
@using (Html.BeginForm()) 
{ 
    ..... 
    @Html.RadioButtonFor(m => m.sort, "date-desc", new { id = "date-desc" }); 
    @Html.RadioButtonFor(m => m.sort, "price-desc", new { id = "price-desc" }); 
    @Html.RadioButtonFor(m => m.sort, "price-asc", new { id = "price-asc" }); 
    @Html.RadioButtonFor(m => m.sort, "like-desc", new { id = "like-desc" }); 
    @Html.RadioButtonFor(m => m.sort, "like-asc", new { id = "like-asc" }); 
    @Html.CheckBoxFor(m => m.show-discount); 
    @Html.CheckBoxFor(m => m.show-new); 
    <input type="submit" class="btn btn-default" value="Search"> 
} 

注意您可以sort和枚舉。如果布爾值可爲空,則CheckBoxFor將呈現帶有3個值的下拉列表(以允許選擇空值)。

還請注意,在您的代碼中,您不需要[Bind(Prefix..。您可以使用<input type="checkbox" name="show-discount" value="true">,但這隻適用於非空的布爾值,因爲未選中的複選框不會回發。如果您不要選擇它,你會得到null,不false

編輯

如果你想使用一個枚舉

public enum MyEnum 
{ 
    date-desc, 
    price-desc, 
    .... 
} 

public class MyModel 
{ 
    public MyEnum sort { get; set; } 
    .... 

視圖(包括@using對於您所定義的組件enum)

@Html.RadioButtonFor(m => m.sort, MyEnum.date-desc, new { id = "date-desc" }); 
@Html.RadioButtonFor(m => m.sort, MyEnum.price-desc, new { id = "date-desc" }); 
.... 
+0

感謝您的回覆,在複選框上使用'value =「true」',就像您現在建議的那樣。第二個問題,是否有可能綁定單選按鈕'排序'與枚舉而不是字符串,但保留值'日期desc,價格desc,價格asc'? – bonjorno 2014-09-29 00:08:39

+0

是的,你可以使用枚舉。我會盡快更新答案(但請注意使用視圖模型和強類型html助手的習慣 - 您將永遠不會遇到這些問題) – 2014-09-29 00:13:02

+0

'(但請注意使用視圖模型和強類型html助手的習慣 - 你永遠不會有這些問題)'我用視圖模型綁定很好,但它是一個特例,它的漫長的故事:)等待更新:) – bonjorno 2014-09-29 00:16:29

0

創建一個ViewModel,它將包含與您的表單輸入相匹配的屬性。

public class ViewModel 
{ 
    public bool ShowDiscount { get; set; } 
    // add additional properties 
} 

然後,在你formview,加@Html.CheckBoxFor(m => m.ShowDiscount)

<form action="/" method="get"> 
    @Html.CheckBoxFor(m => m.ShowDiscount) 
    <input type="submit" class="btn btn-default" value="Search"> 
</form> 

一定要讓view知道應該期待哪種類型的model。在view的頂部指定。

@model YourNamespace.ViewModel 

controller方法現在看起來就像這樣:

public ActionResult Index(ViewModel v) 
{ 
    // do work here. 
} 

框架將做結合爲您服務。

+0

感謝您的回覆,我很瞭解視圖模型綁定,但我不喜歡與默認屬性名稱綁定,我的行爲將從外部網站調用,它應該能夠通過get方法從表單調用。這是關於眼睛糖果的事情。 – bonjorno 2014-09-29 00:13:04