如何在Viewbag中绑定列表值(数组)

时间:2017-06-27 08:05:07

标签: c# asp.net-mvc

这里我在viewbag中获取值但我无法将值绑定到fields.And这是我的代码

@foreach (RankedServices.Business.UIModels.Services cc in ViewBag.selectedCommonCriteria)
                    {
                        <div class="row" style="margin-top:10px;">
                            <div class="col-lg-5">
                                <div class="form-group">
                                    <label class="control-label col-sm-12">Awards:</label>
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="Awards" id="Awards" placeholder="Enter Awards" value="@cc.SelectedCC[0].Awards"/>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-5">
                                <div class="form-group">
                                    <label class="control-label col-sm-12">Certifications:</label>
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="Certifications" id="Certifications" placeholder="Enter Certifications" value="@cc.SelectedCC[0].Certifications" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row" style="margin-top:10px;">
                            <div class="col-lg-5">
                                <div class="form-group">
                                    <label class="control-label col-sm-12">Associations:</label>
                                    <div class="col-sm-12">
                                        <input type="text" class="form-control" name="Associations" id="Associations" placeholder="Associations" value="@cc.SelectedCC[0].Associations"/>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-6">
                                <div class="form-group">
                                    <label class="control-label col-sm-12">Share your areas of expertise and how you can best help customers:</label>
                                    <div class="col-sm-12">
                                        <textarea class="form-control" name="AdditionalInfo" id="AdditionalInfo" placeholder="Please tell your customers about your interests, specialities and expertise" rows="2" value="@cc.SelectedCC[0].AdditionalInfo"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    }

我收到错误:&#34;无法隐式转换类型&#39; RankedServices.Business.UIModels.Services&#39;到&#39; System.Collections.IEnumerable&#39;。存在显式转换(您是否错过了演员?)&#34;

我应该如何约束这些声音。

1 个答案:

答案 0 :(得分:0)

假设您的ViewBag.selectedCommonCriteria是[{1}}类型IEnumerable,那么您可以这样做:

Services

你正在使用@foreach (RankedServices.Business.UIModels.Services cc in (IEnumerable<RankedServices.Business.UIModels.Services>)ViewBag.selectedCommonCriteria) 这是一个动态对象,所以为了将它用于除了每个对象之外的任何东西(例如ViewBag),你必须将它强制转换为适当的类型