我正在尝试为应用程序的一部分构建搜索引擎,我需要帮助。我正在使用 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>();
对象。AStoreViewModel
是这种对象:
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; }
所以,是的,我没有使用构建视图模型列表的方法,而是为我拥有的每个商店创建了单个布尔值。所以基本上我的视图现在呈现如下:
<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>
而且我确实得到了正确的检查值,但它很难看,每次我有一个新商店时我都需要去修改它。所以,基本上,我的问题仍然没有答案......