如何将动态数据作为数组从数据库从控制器发送到.net mvc5中查看?

时间:2017-12-13 05:35:44

标签: .net asp.net-mvc-5

This is what I got on the web:

In Controller:
 int count = ems.SupportAdds.Where(x=>x.ArticleId == id).Count();

                  var stringArray = new string[count]{"",""}; //Here,I want to fetch data from table.

                  ViewBag.Collection = stringArray;

But, I want to send data dynamically which will come from the database using entity framework.

In View :

           @for (int i = 0; i <= Count; i++)
            {
           @Html.LabelFor(model => @item.supportAdd.Title)
          <input type="text" name="Title[]" id="HeadingTitle" 
            Value="@Html.Raw(ViewBag.Collection[i])"/>
            }

我没有得到控制器如何将数据发送到视图的逻辑。

我无法发送数据集来查看。

以前,我习惯使用viewbag发送,但这是运行时绑定错误。

我不知道如何解决它。

2 个答案:

答案 0 :(得分:0)

要将任何集合值传递给View,您可以使用foreach获取每个值

控制器:

 var stringArray = new string[]
 {
 "Manual", "Semi", "Auto", "SSS"
 };
 ViewBag.Collection = stringArray;

查看

foreach(var p in ViewBag.Collection)
{
@for (int i = 0; i <= Count; i++)
{
@Html.LabelFor(model => @item.supportAdd.Title)
<input type="text" name="Title[]" id="HeadingTitle" value="@p"/>
}
} 

- 此代码未经过测试

答案 1 :(得分:0)

UINavigationBar.appearance().backIndicatorImage = image.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image.withRenderingMode(.alwaysOriginal)

    if #available(iOS 11, *) {
        UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
        UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)
    } else {
        UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -60, vertical: -60), for: .default)
    }