Html table not populating from ViewModel

时间:2015-07-31 19:53:13

标签: c# sql asp.net-mvc viewmodel

I am trying to populate an HTML table with data from a table in my database. The issue is simply that the HTML table is not getting populated with any data.

Here is the ViewModel:

    public class TestViewModel
{
    public string MatchedId { get; set; }
    public string UnmatchedId { get; set; }
    public string Auth { get; set; }
    public DateTime CreditDate { get; set; }
    public string CreditNumber { get; set; }
    public decimal CreditAmount { get; set; }
    public DateTime DeniedDate { get; set; }
    public int DeniedReasonId { get; set; }
    public string DeniedNotes { get; set; }
}

Controller Action:

   [HttpPost]
public ActionResult UploadValidationTable(HttpPostedFileBase csvFile)
{
    var inputFileDescription = new CsvFileDescription
    {
        SeparatorChar = ',',
        FirstLineHasColumnNames = true
    };
    var cc = new CsvContext();
    var filePath = uploadFile(csvFile.InputStream);
    var model = cc.Read<Credit>(filePath, inputFileDescription);

    try
    {
        var entity = new Entities();

//model here is the .csv, doesn't have anything to do with this issue
        foreach (var item in model)
        {
            var tc = new TemporaryCsvUpload
            {
                Id = item.Id,
                CreditAmount = item.CreditAmount,
                CreditDate = item.CreditDate,
                CreditNumber = item.CreditNumber,
                DeniedDate = item.DeniedDate,
                DeniedReasonId = item.DeniedReasonId,
                DeniedNotes = item.DeniedNotes
            };
            entity.TemporaryCsvUploads.Add(tc);
        }
        entity.SaveChanges();

        System.IO.File.Delete(filePath);

//This is where the database table is getting filled
 entity.Database.ExecuteSqlCommand("Insert into CsvReport Select  p.Id as MatchedId, case when p.Id is null then t.Id end as UnmatchedId, p.Auth,p.CreditDate, p.CreditNumber,p.CreditAmount, p.DeniedDate,p.DeniedReasonId, p.DeniedNotes from TemporaryCsvUpload t left join PermanentTable p on p.Id = t.Id;");


        TempData["Success"] = "Updated Successfully";

    }
    catch (LINQtoCSVException)
    {
        TempData["Error"] = "Upload Error: Ensure you have the correct header fields and that the file is of .csv format.";
    }

    return View("Upload");
}

View:

@model IEnumerable<TestProject.TestViewModel>

                        @if (Model != null)
                    {
                        foreach (var item in Model.Where(x => x.IdMatched != null))
                        {
                            <tr>
                                <td>
                                    @item.MatchedId
                                </td>
                                <td>
                                    @item.Auth
                                </td>
                                <td>
                                    @item.CreditDate
                                </td>
                                <td>
                                    @item.CreditNumber
                                </td>
                                <td>
                                    @item.CreditAmount
                                </td>
                                <td>
                                    @item.DeniedDate
                                </td>
                                <td>
                                    @item.DeniedReasonId
                                </td>
                                <td>
                                    @item.DeniedNotes
                                </td>
                            </tr>
                        }
                    }

It's a little weird because I am populating the database with an SQL command. What am I missing here? Do I need to try and pass it through the controller action? Let me know if you need more information. Thanks!

Edit

I tried to pass the instance through, but I may still be doing it incorrectly:

         var testModel = new TestViewModel();

         return View("Upload", testModel);

Here is what its padding through:

    public class TestViewModel
{
    public IEnumerable<Test> Test { get; set; } 
}

2 个答案:

答案 0 :(得分:0)

Made an answer so essentially the view doesn't know what to render you need to pass an actual filled model (in your case an IEnumerable to the view). This can be done using the method:

View("Upload", viewModelList);

Controller.View docs on MSDN

答案 1 :(得分:0)

您似乎没有向视图模型添加任何数据。 如果您的视图模型是Test对象的集合,则需要添加一些 测试集合的对象。

var model = new TestViewModel()
{
    Test = new List<Test>() { new Test(), new Test(), ... }
}
return View("Upload", model);