My CREATE.cshtml view page not opening

Emmanuel2103 1 Reputation point
2022-07-14T06:35:28.323+00:00

I am creating an MVC application, the application runs successfully but whenever i want to add an actor (based on project) the create view page does not work. Its showing internal server error.

This is the error it is bringing;

AspNetCoreGeneratedDocument.Views_Actors_Create.ExecuteAsync() in Create.cshtml

@model Actor  
@{  

    ViewData["Title"] = "Add New Actor";  

}  
<div class="row text">  
    <div class="col-md-8 offset-2">  
        <p>  
            <h1>Add New Actor</h1>  

The error is being highlighted on the ViewData line.

My create.cshtml

@model Actor  
  
@{  
    ViewData["Title"] = "Add New Actor";  
}  
  
<div class="row text">  
    <div class="col-md-8 offset-2">  
        <p>  
            <h1>Add New Actor</h1>  
        </p>  
        <div class="row">  
            <div class="col-md-8 offset-2">  
                <form class="row" asp-action="Create">  
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  
                    <div class="mb-3">  
                        <label asp-for="ProfilePictureURL" class="form-label"></label>  
                        <input asp-for="ProfilePictureURL" type="text" class="form-control"/>  
                        <span asp-validation-for="ProfilePictureURL" class="text-danger"></span>  
                    </div>  
  
                    <div class="mb-3">  
                        <label asp-for="FullName" class="form-label"></label>  
                        <input asp-for="FullName" type="text" class="form-control"/>  
                        <span asp-validation-for="FullName" class="text-danger"></span>  
                    </div>  
  
                    <div class="mb-3">  
                        <label asp-for="Bio" class="form-label"></label>  
                        <input asp-for="Bio" type="text" class="form-control"/>  
                        <span asp-validation-for="Bio" class="text-danger"></span>  
                    </div>  
  
                    <div class="mb-3">  
                        <input type="submit" value="Create" class="btn btn-outline-success float-end"/>  
                        <a class="btn btn-outline-secondary" asp-action="Index"> Show All</a>  
  
                    </div>  
  
                </form>  
  
            </div>  
        </div>  
    </div>  
  
</div>  

This is my ActorsController.cs

using eTickets.Data;  
using eTickets.Data.Services;  
using Microsoft.AspNetCore.Mvc;  
  
namespace eTickets.Controllers  
{  
    public class ActorsController : Controller  
    {  
        private readonly IActorsService _service;  
  
        public ActorsController(IActorsService service)  
        {  
            _service = service;  
        }  
  
        public async Task<IActionResult> Index()  
        {  
            IEnumerable<Models.Actor>? data = await _service.GetAll();  
            return View(data);  
        }  
        public IActionResult Create()  
        {  
            return View();  
        }  
    }  
}  

What could be the problem? Thanks in advance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,246 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,205 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 4,201 Reputation points Microsoft Vendor
    2022-07-18T02:43:44.27+00:00

    Hi @Emmanuel2103

    I think what you want is something like the following, please check it.

    221664-9.gif

    My project structure.

    ![221692-image.png]2

    Test Code

    public IActionResult CreateOrUpdate(string id)  
            {  
                if (id == null || id.Equals(string.Empty))  
                {  
                    // Create a new model  
                    ViewBag.Title = "Create a new model";  
                    return View();  
                }  
                else {  
                    // Update the exist model  
                    Actor editModel = new Actor  
                    {  
                        ProfilePictureURL="test.url",  
                        FullName= "test FullName",  
                        Bio = "test Bio",  
                        Id = "001"  
                    };  
                    ViewBag.Title = "Update exist Actor model";  
                    return View(editModel);  
    
                }  
    
            }  
    

    CreateOrUpdate.cshtml

    @model Actor  
    
    @{  
        ViewData["Title"] = ViewBag.Title;  
    }  
    
    <div class="row text">  
        <div class="col-md-8 offset-2">  
            <p>  
                <h1>@ViewBag.Title</h1>  
            </p>  
            <div class="row">  
                <div class="col-md-8 offset-2">  
                    <form class="row" asp-action="Create">  
                        <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
    
                        <div class="mb-3">  
                            <label asp-for="ProfilePictureURL" class="form-label"></label>  
                            <input asp-for="ProfilePictureURL" type="text" class="form-control" />  
                            <span asp-validation-for="ProfilePictureURL" class="text-danger"></span>  
                        </div>  
    
                        <div class="mb-3">  
                            <label asp-for="FullName" class="form-label"></label>  
                            <input asp-for="FullName" type="text" class="form-control" />  
                            <span asp-validation-for="FullName" class="text-danger"></span>  
                        </div>  
    
                        <div class="mb-3">  
                            <label asp-for="Bio" class="form-label"></label>  
                            <input asp-for="Bio" type="text" class="form-control" />  
                            <span asp-validation-for="Bio" class="text-danger"></span>  
                        </div>  
    
                        <div class="mb-3">  
                            <input type="submit" value="Create" class="btn btn-outline-success float-end" />  
                            <a class="btn btn-outline-secondary" asp-action="Index"> Show All</a>  
    
                        </div>  
    
                    </form>  
    
                </div>  
            </div>  
        </div>  
    
    </div>  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards
    Jason

    0 comments No comments