将信息从控制器移动到视图

时间:2016-08-31 12:47:37

标签: c# asp.net-mvc mongodb

我需要将数据库中读取的数据从控制器移动到视图并将其显示到表中。我可以成功地从数据库中读取商店信息到对象,但是当我运行页面时,我收到@foreach (var item in Model)错误。以下是读取数据库的方法:

public class DatabaseRead
{
    public static async Task MongoReader(string path)
    {
        {
            MongoClient client = new MongoClient();
            var db = client.GetDatabase("POWA");
            var collection = db.GetCollection<files>("Imported");
            var filter = Builders<files>.Filter.Eq("quote_number", path);
            var result = await collection.Find(filter).ToListAsync();
            foreach (var results in result)
                {
                    ContentDisplay read = new ContentDisplay();
                    read.product_name = results.product_name;
                    read.catalog_number = results.catalog_number;
                }
        }
    }
}

这是我的观点:

@model List<ProductionOrderWebApp.Controllers.ContentDisplay>
@{ ViewBag.Title = "Display"; }

<h2>Order Table>
<table board="1", style ="width:auto">
<tr>
    <th>Item Name</th>
    <th>Catalog Number</th>
</tr>
@foreach (var item in Model)
{
<tr>
    <th>@Html.Display(item.product_name);</th>
</tr>
}

如何让表格显示对象的所有条目而不会收到错误?

修改

这是我的控制器的代码。它所做的只是调用两个方法,一个读取CSV文件并写入mongo,另一个读取数据库并尝试显示内容。

namespace ProductionOrderWebApp.Controllers
pubic class Homecontroller : Controller
 {
    public ActionResult Index
    {
        return View();
    }

    public ActionResult Display()
    {
        return View();
    }


    [HttpPost]
    public async Task<ActionResult> Index(HttpPostedFileBase file)

    {
        if (file != null && file.ContentLength > 0)
        {
            var fileName = System.IO.Path.GetFileName(file.FileName);
            var path = System.IO.Path.Combine(("C:\\Dev\\ProductionOrderWebApp\\Uploads"), fileName);
            file.SaveAs(path);
            await CSVRead.CSVReader(path); //calls a method that reads and takes apart a CSV file
            await DatabaseRead.MongoReader(path);

        }
        return View("Display");
    }
}

}

2 个答案:

答案 0 :(得分:0)

为什么每次在循环中创建ContentDisplay的新对象,因此,您没有将所需数据传递给视图,

data my_data_with_months;
    set my_data;
    MONTH = INTNX('month', NORMAL_DATE, 0, 'B');
run;

答案 1 :(得分:0)

首先,DatabaseRead.MongoReader需要返回Task<List<ContentDisplay>>而不是任何内容。在该方法中,您需要在foreach循环之外声明返回列表并在其中添加并返回

var returnList = new List<ContentDisplay>();
foreach (var results in result)
{
    var read = new ContentDisplay();
    read.product_name = results.product_name;
    read.catalog_number = results.catalog_number;
    returnList.add(read);
}

return returnList;

然后代替

await DatabaseRead.MongoReader(path);

你需要实际抓住它才能使用

var model = await DatabaseRead.MongoReader(path);

然后将其传递给视图

return View("Display", model);

<强> CAVEATS

我没有做过很多异步等待事情,所以返回列表可能会有些麻烦。

我担心await CSVRead.CSVReader(path);在您期望它做某事时可能也无所作为。

修改

public async Task<ActionResult> Index(HttpPostedFileBase file)
{
    List<ContentDisplay> model = new List<ContentDisplay>();
    if (file != null && file.ContentLength > 0)
    {
        var fileName = System.IO.Path.GetFileName(file.FileName);
        var path = System.IO.Path.Combine(("C:\\Dev\\ProductionOrderWebApp\\Uploads"), fileName);
        file.SaveAs(path);
        await CSVRead.CSVReader(path); //calls a method that reads and takes apart a CSV file
        model = await DatabaseRead.MongoReader(path);

    }
    return View("Display", model);
}