我有一個從像下面綁定參數與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)
{
}
問題是ShowDiscount
和ShowNew
參數總是空(沒有正確綁定)。
我認爲這個複選框造成的問題檢查時寫爲cb-name=on
而不是cb-name=true
。並且當複選框未被選中時,它不會寫在url上。
有沒有適當的方法來做到這一點?
有沒有辦法將Sort
參數映射到枚舉?
使用視圖模型和強類型化html helper,並讓MVC爲您完成所有工作。 – 2014-09-28 23:27:24