我有一个像下面这样的ViewModel,
public class EnrollPlanPriceLevelViewModel
{
    public EnrollPlanPriceLevel EnrollPlanPriceLevels { get; set; }        
    public Dictionary<PriceLevel,decimal> Prices { get; set; }
}我的视图中的代码,但我无法创建价格视图。请帮帮我!
 @{
int i = 0;
foreach (FCA.Model.PriceLevel p in ViewBag.PriceLevelID)
{
    i = i + 1;
                    <div class="span4">                            
                        @Html.TextBoxFor(model => model.Prices[p], new { @placeholder = "Price" })
                    </div>
}
                }我想如何在Controller中调用Dictionary对象值?
发布于 2013-12-03 20:55:07
ViewModel:
public class EnrollPlanPriceLevelViewModel
{
    public EnrollPlanPriceLevel EnrollPlanPriceLevels { get; set; }
    public Dictionary<string,decimal> Prices { get; set; }      
}我的控制器的Get方法应该初始化'Key‘和值,如下所示。以便您可以在视图中为每个KeyValue对进行循环。
控制器的GET方法:
 public ActionResult Create()
    {
        var model = new EnrollPlanPriceLevelViewModel();
        model.Prices = new Dictionary<string, decimal>();
        foreach (PriceLevel p in db.PriceLevelRepository.GetAll().ToList())
        {
            model.Prices.Add(p.PriceLevelID.ToString(), 0);
        }            
        return View(model);
    }使用字典查看,如下所示:
 <div class="span12">
                @{           
foreach (var kvpair in Model.Prices)
{        
                    <div class="span4">
                        @Html.TextBoxFor(model => model.Prices[kvpair.Key], new { @placeholder = "Price" })
                         @Html.ValidationMessageFor(model => model.Prices[kvpair.Key])
                    </div>
}
                }
            </div>控制器的POST方法:显示字典的值

发布于 2013-12-03 15:58:50
prices是一个字典项(例如KeyValuePair<PriceLevel, decimal>)。
因此,必须分别绑定该对的每个属性:Key和Value。
https://stackoverflow.com/questions/20343480
复制相似问题