ASP.NET MVC is the best approach for presenting data in Views

I would like to provide data to a table in views. Data not only from the database but also from the csv file.

Should I store data in ViewData or store it in an object and pass it as? What is the best approach or any other methods I can use? thanks!

0


a source to share


2 answers


Use strongly typed views and pass the object directly to the view:



// Model (PersonRepository class)
public static Person Get(Int32 id) {
  using (MyContext context = new MyContext()) {
    Person p = context.Person.First(p => Person.id == id);
    return p;
  }
}

...

// Controller
public ActionResult Show(Int32 id) {
  return View(PersonsRepository.Get(id);
}

...

// View
<%@ Page Inherits="System.Web.Mvc.ViewPage<Models.Person>" Title="" Language="C#" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

  <%= Model.Id %> <br />
  <%= Model.Name %> <br />

</asp:Content>

      

+1


a source


You must create a model object, fill it in the controller with data from heterogeneous sources, and pass that model to the view.



0


a source







All Articles