I am experiencing with the C#/.Net
world using Azure Blobs
.
I made a Gallery that takes the images from an Azure Blob Storage, and displays it on the screen.
I want to create a Partial View
from it, because of the DRY
principle so I could reuse it in other files as well.
When I display the Partial View on the index it works well, but when I try to display it in the Info.cshtml
file doesn't find the Model
variable.
Controller
public class ResourceController : Controller
{
const string blobContainerName = "blobContainer";
static BlobContainerClient blobContainer;
private IConfiguration _configuration;
public ResourceController(IConfiguration configuration)
{
_configuration = configuration;
}
// Create a List of images using Azure Blobs and Displays it on the Screen.
{
try
{
var s = _configuration.GetConnectionString("AzureConnectionString");
BlobServiceClient blobServiceClient = new BlobServiceClient(s);
blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName);
await blobContainer.CreateIfNotExistsAsync(PublicAccessType.Blob);
List<Uri> allBlobs = new List<Uri>();
foreach (BlobItem blob in blobContainer.GetBlobs())
{
if (blob.Properties.BlobType == BlobType.Block)
allBlobs.Add(blobContainer.GetBlobClient(blob.Name).Uri);
}
return View(allBlobs);
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
ViewData["trace"] = ex.StackTrace;
return View("Error");
}
}
public ActionResult Info(){
//This class doesn't see the Model variable
return View();
}
Partial
//The Info class Doesn't see the Model variable
@if (Model != null && Model.Count > 0)
{
foreach (var item in Model)
{
<div class="imageBlock" value="GetGallery">
<a href="@item" target="_blank"><img class="thumb" src="@item" alt="images"/></a><br />
<div class="deleteDiv"><img class="deleteIcon" src="~/Images/deleteImage.png" title="Delete Image" onclick="deleteImage('@item');" /></div>
</div>
}
}
ViewModel
using Microsoft.AspNetCore.Mvc;
namespace AzureBlobLearning.Models
{
public class ResourceViewModel
{
public string? RequestId { get; set; }
public bool? ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
Index.csthml
//This displays the list of images
<partial name="~/Views/Resource/_ResourcePartial.cshtml" />
Info.cshtml
//This doesn't display
<partial name="~/Views/Resource/_ResourcePartial.cshtml" />
My Question:
PS.: If you have any questions feel free to ask.
Because the action is just returning the View, with no Model inside.
Change from this:
public ActionResult Info(){
//This class doesn't see the Model variable
return View();
}
to this:
public ActionResult Index(){
var model = LoadModelMethod();
return View(model);
}
public ActionResult Info(){
var model = LoadModelMethod();
return View(model);
}
public List<Uri> LoadModelMethod()
{
try
{
var s = _configuration.GetConnectionString("AzureConnectionString");
BlobServiceClient blobServiceClient = new BlobServiceClient(s);
blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName);
await blobContainer.CreateIfNotExistsAsync(PublicAccessType.Blob);
List<Uri> allBlobs = new List<Uri>();
foreach (BlobItem blob in blobContainer.GetBlobs())
{
if (blob.Properties.BlobType == BlobType.Block)
allBlobs.Add(blobContainer.GetBlobClient(blob.Name).Uri);
}
return allBlobs;
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
ViewData["trace"] = ex.StackTrace;
throw;
}
}
you can do what I said above, extract the logic which loads the images to a method, and call it in both actions.