我正在尝试为应用程序的一部分构建搜索引擎,我需要帮助。我正在使用MVC 4,我不知道为什么我的checkboxfor总是返回“true”值。
我正在使用这些不同的项目:
首先,搜索模型:
public class SearchBackstoreInventoryPartial
{
[RegularExpression(@"^[a-zA-Z0-9\',;/\)\(\-\. ]*$", ErrorMessage = "Input is invalid.")]
public string mCardName { get; set; }
public decimal mBackstorePrice { get; set; }
public string mBackstorePriceSymbol { get; set; }
public int mBackstoreQty { get; set; }
public string mBackstoreQtySymbol { get; set; }
public int mBackstoreSoldQty { get; set; }
public string mBackstoreSoldSymbol { get; set; }
public decimal mBackstoreDiscount { get; set; }
public string mBackstoreDiscountSymbol { get; set; }
public IEnumerable<SelectListItem> mSymbolList { get; set; }
public List<StoreViewModel> mListStores = new List<StoreViewModel>();
private readonly StoresManager mStoresManager = new StoresManager();
public SearchBackstoreInventoryPartial()
{
mSymbolList = new SelectList(ValueDomain.LIST_METHODS);
List<StoreInfo> listStores = mStoresManager.ListStores();
foreach (StoreViewModel itemToAdd in listStores.Select(_store => new StoreViewModel
{
mStore = _store,
mIsSelected = true
}))
{
mListStores.Add(itemToAdd);
}
}
你可以忽略几乎所有的东西,我主要关注public List<StoreViewModel> mListStores = new List<StoreViewModel>();
对象。 StoreViewModel
就是这种对象:
public class StoreViewModel
{
public StoreInfo mStore { get; set; }
public bool mIsSelected { get; set; }
}
所以,基本上,对于我的应用程序所拥有的每个商店,我都存储了它,并将mIsSelected放到true
。
然后我渲染一个这样的页面:
@model MvcMagicAdmin.Utilities.SearchModels.SearchBackstoreInventoryPartial
@using(Html.BeginForm())
{
<div class="formStyle defaultBaseStyle baseFontSize">
<div class="center">
<div>
<div class="float-left">Stores:</div>
<div class="float-right">
@for (int i = 0; i < Model.mListStores.Count; i++)
{
<div class="float-left"style="padding: 1px; margin: 1px">
@Model.mListStores[i].mStore.mStoreName
</div>
<div class="float-right"style="padding: 1px; margin: 1px">
@Html.CheckBoxFor(_item => _item.mListStores[i].mIsSelected)
</div>
<div class="clear"></div>
}
</div>
<div class="clear"></div>
</div>
<div>
<div class="float-left">
Card Name:
</div>
<div class="float-right">
@Html.TextBoxFor(_item => _item.mCardName)
@Html.ValidationMessageFor(_item => _item.mCardName)
</div>
<div class="clear"></div>
</div>
<div>
<div class="float-left">
Backstore Item Price
</div>
<div class="float-right">
@Html.DropDownListFor(_item => _item.mBackstorePriceSymbol, Model.mSymbolList, "---Select a value---")
@Html.TextBoxFor(_item => _item.mBackstorePrice, new { @class = "positive" } )
</div>
<div class="clear"></div>
</div>
<div>
<div class="float-left">
Backstore Quantity in stock
</div>
<div class="float-right">
@Html.DropDownListFor(_item => _item.mBackstoreQtySymbol, Model.mSymbolList, "---Select a value---")
@Html.TextBoxFor(_item => _item.mBackstoreQty, new { @class = "positive-integer" } )
</div>
<div class="clear"></div>
</div>
<div>
<div class="float-left">
Backstore Item sold
</div>
<div class="float-right">
@Html.DropDownListFor(_item => _item.mBackstoreSoldSymbol, Model.mSymbolList, "---Select a value---")
@Html.TextBoxFor(_item => _item.mBackstoreSoldQty, new { @class = "positive-integer"})
</div>
<div class="clear"></div>
</div>
<div>
<div class="float-left">
Backstore Item discount
</div>
<div class="float-right">
@Html.DropDownListFor(_item => _item.mBackstoreDiscountSymbol, Model.mSymbolList, "---Select a value---")
@Html.TextBoxFor(_item => _item.mBackstoreDiscount, new { @class = "positive"})
</div>
<div class="clear"></div>
</div>
</div>
<input type="submit" value="Go" class="buttonField"/>
<div class="clear"></div>
</div>
}
然后,我的控制器方法:
[HttpPost]
public ActionResult SearchBackstoreInventory(SearchBackstoreInventoryPartial _searchBackstore)
{
// Do some work here...
}
默认情况下会选中复选框,但尽管我取消选中它们,但值仍为true。任何人都可以帮我解释为什么这不起作用?
修改
以下是我视图中循环的渲染:
<div class="float-right">
<div class="float-left"style="padding: 1px; margin: 1px">
FinePlaySet.com
</div>
<div class="float-right"style="padding: 1px; margin: 1px">
<input checked="checked" id="mListStores_0__mIsSelected" name="mListStores[0].mIsSelected" type="checkbox" value="true" /><input name="mListStores[0].mIsSelected" type="hidden" value="false" />
</div>
<div class="clear"></div>
<div class="float-left"style="padding: 1px; margin: 1px">
EbayAuctions
</div>
<div class="float-right"style="padding: 1px; margin: 1px">
<input checked="checked" id="mListStores_1__mIsSelected" name="mListStores[1].mIsSelected" type="checkbox" value="true" /><input name="mListStores[1].mIsSelected" type="hidden" value="false" />
</div>
<div class="clear"></div>
<div class="float-left"style="padding: 1px; margin: 1px">
Strawberry Fields
</div>
<div class="float-right"style="padding: 1px; margin: 1px">
<input checked="checked" id="mListStores_2__mIsSelected" name="mListStores[2].mIsSelected" type="checkbox" value="true" /><input name="mListStores[2].mIsSelected" type="hidden" value="false" />
</div>
<div class="clear"></div>
</div>
编辑2
我尝试过不同的方式构建我的模型,如下所示:
public bool mFinePlaySet { get; set; }
public bool mEbayAuctions { get; set; }
所以,是的,我没有建立视图模型列表的方法,而是为每个商店创建了单个bool。所以基本上我的视图现在呈现如下:
<div>
<div class="float-left">Stores:</div>
<div class="float-right">
<p>FinePlaySet.com : @Html.CheckBoxFor(_item => _item.mFinePlaySet)</p>
<p>eBay Auctions: @Html.CheckBoxFor(_item => _item.mEbayAuctions)</p>
</div>
<div class="clear"></div>
</div>
我确实得到了正确的检查值,但它很难看,每次我有新店时我都需要去修改它。所以,基本上,我的问题仍然没有答案......