Creating a Project in Visual Studio Part II
-
Creating a Project in Visual Studio Part I
Models
Now we get into connecting to the data source, our Azure Table. As mentioned previously, the models are where we retrieve the external date in an MVC app. Your standard model represents a single object, in our example a Task. The model is passed to the view to be displayed. A collection of models can also be passed to the view. Remember, the model classes are the data layer. The controller and the view have no idea where the data actually resides, and you can always update the models to point to another source without restructuring the entire application.
There are two classes we are going to work with, Task and Tasks. Task contains the properties of a single object, and usually doesn't have methods. In the schema I have designed, the Tasks class is responsible for talking to the data and populating the Task.
- We will start with the TaskModel class. In Visual Studio, from Solution Explorer, right-click on Models and click "Add | New Item..." It is important to choose the correct folder so your new item will be stored in the right location. In the Add New Item dialog window, enter "TaskModel.cs" and click "Add".
- You now have a new class TaskModel with the namespace "Tasks.Models". Within the class, add a property definition for each field in your Azure Table. In our example, we have the following:
public string PartitionKey { get; set; } = "Tasks"
public string? RowKey { get; set; }
public string? AssignedTo{ get; set; }
public DateTime DueDate{ get; set; }
public string? Status{ get; set; } - As you can see, each property has a get and set method built in. The PartitionKey is not going to be modified by the app, it will always be "Tasks". Each string is defined as string? so a null value can be stored.
- Next is the TaskModels class. From Solution Explorer, right-click on Models and click "Add | New Item..." In the Add New Item dialog window, enter "TaskModels.cs" and click "Add". You will need to create a method for each step in working with Tasks, such as Add, Edit, and so forth. For example:
public static string? AddItem(SpeakerModel model)
{
var tableEntity = Populate(model);GetTableClient().AddEntity(tableEntity);
return model.RowKey;
} - Open the HomeController.cs file and add a new IActionResult method called List. This will display a list of all Tasks:
public IActionResult List(string search)
{
try
{
ViewBag.Search = search;
return View(SpeakerModels.GetList(search));
}
catch (Exception ex)
{
ViewBag.Error = ex.Message;
return View(new SpeakerModel());
}
} - Right-lick on "List" and click "Add View..."
- From the Add New Scaffolded Item dialog window, click "Razor View" and click "Add" (or double-click "Razor View").
- From the Add Razor View dialog window, note the View name "List" is already populated. Change Template to "List" and Model class to "TaskModel" and click "Add". Visual Studio will create a new List.cshtml file that accepts a TaskModel for input, which will display all Tasks in the Azure Table.
- Open the _Layout.cshtml file and locate the <li> list items. Copy and paste the Privacy list item and change both asp-action and the link text from "Privacy" to "List". This will display the List.cshtml file.
- Saturday, 13 September 2025 03:17
- Category
- Keywords
- Private
- Azure
- Development