加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – 如何在MVC4 ViewModel,Controller,View中使用D

发布时间:2020-12-16 07:17:47 所属栏目:asp.Net 来源:网络整理
导读:我有一个像下面的ViewModel, public class EnrollPlanPriceLevelViewModel{ public EnrollPlanPriceLevel EnrollPlanPriceLevels { get; set; } public DictionaryPriceLevel,decimal Prices { get; set; }} 我的视图中的代码,但我无法创建查看价格.请帮帮我
我有一个像下面的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对象值如何?

解决方法

ViewModel:

public class EnrollPlanPriceLevelViewModel
{

    public EnrollPlanPriceLevel EnrollPlanPriceLevels { get; set; }
    public Dictionary<string,decimal> Prices { get; set; }      
}

我的Controller的Get方法应该像这样初始化’Key’和Values.这样您就可以在View中遍历每个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);
    }

像这样使用Dictionary查看:

<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>

Controller的POST方法:显示字典的值

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读