Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the view model null?

I have the following structure:

Controller.cs

public ActionResult PageMain(string param)
{
    return View();
}

PageMain.cs

namespace project1.Models
{
    public class PageMain
    {
        public DataTable dtable
        { 
             get {
                       // some code that returns a DataTable
             }
        }
     }
}

and finally in the view:

@using project1.Models
@model PageMain

var datatable = Model.dtable // but this is throwing an error since the model is null

Does anyone know why my model is returning null? How can I access the datatable in the PageMain.cs? I am new to MVC so if I have any logical error in the structure please do not hesitate in warning me :)

like image 806
Shaokan Avatar asked Jun 05 '11 12:06

Shaokan


People also ask

How do you deal with null models?

In order to simplify your code you should utilize the Null Object pattern. Instead of using null to represent a non existing value, you use an object initialized to empty/meaningless values. This way you do not need to check in dozens of places for nulls and get NullReferenceExpections in case you miss it.

What is a ViewModel used for?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

What is the difference between View and ViewModel?

VIEW: ( Platform Specific Code – USER INTERFACE ) What the user sees, The Formatted data. VIEWMODEL: ( Reusable Code – LOGIC ) Link between Model and View OR It Retrieves data from Model and exposes it to the View. This is the model specifically designed for the View.

What is the use of ViewModel in MVC?

What ViewModel is. In ASP.NET MVC, ViewModels are used to shape multiple entities from one or more models into a single object. This conversion into single object provides us better optimization.


2 Answers

First, you need to set your logic to reach the database form your model. You could use ORM to achieve that.

Then, pass your model to view from your controller. Assume that you have your person model like below:

public class Person {

    public string Name {get;set;}
    public string SurName {get;set;}
    public int Age {get;set;}

}

In order to view specific Person data, you need to query your model and pass this model from you controller to your view:

public ActionResult Index() {

  var _entities = new MyDataEntity();
  var model = _entities.Person;
  model = model.Where(x => x.Age > 20);

  return View(model);

}

The above controller is passing List of Person to your view. MyDataEntity class is your entity framework DataContext class.

After that you need to put @model IEnumerable<Person> inside your model. Here is an example:

@model IEnumerable<MyApp.Models.Person>

<ul>
@foreach(var item in Model){

  <li>Name : @item.Name</li>
  <li>Surname : @item.Surname</li>
  <li>Age : @item.Age</li>

}

</ul>
like image 185
tugberk Avatar answered Nov 16 '22 02:11

tugberk


You need to create a model in your controller to pass to the View().

like image 29
SLaks Avatar answered Nov 16 '22 03:11

SLaks