etrupja / complete-ecommerce-aspnet-mvc-application

This is the source code of the "ASP.NET MVC | Build a Complete eCommerce App" course on Udemy
https://bit.ly/3CWvkqk
261 stars 300 forks source link

Add Controller Doesn't Work #8

Closed sidqiaverroes closed 1 year ago

sidqiaverroes commented 1 year ago

I followed the tutorial from Udemy while looking at the migration notes. After completing section 32 "Model Validation" and run the app, there is no error appear in the Visual Studio. I tried the add actor functionality, but nothing happened. How do I debug this error?

JamesOmego commented 1 year ago

Same problem here.

@sidqiaverroes Did you finally figure it out?

Any hints @etrupja

dwarwick commented 1 year ago

I am not the author, but I will try to help. Can you please post your Actor model code and the full form code?

I'm thinking that either your model properties do not have the Data Annotations or the form does not have the asp-for attributes properly setup, or they are missing from the form.

JamesOmego commented 1 year ago

Thanks @dwarwick

These are my files: [Models] using System.ComponentModel.DataAnnotations;

namespace eTickets.Models { public class Actor { [Key] public int Id { get; set; }

    [Display(Name ="Profile Picture")]
    [Required(ErrorMessage = "Profile Picture is Required")]
    public string ProfilePictureURL { get; set; }

    [Display(Name = "Full Name")]
    [Required(ErrorMessage = "Full Name is Required")]
    [StringLength(50,MinimumLength =3,ErrorMessage ="Full Name must be between 3 and 50 Characters")]
    public string FullName { get; set; }

    [Display(Name = "Biography")]
    [Required(ErrorMessage = "Biography is Required")]
    public string Bio { get; set; }

    //DB Relationship

    public List<Actor_Movie> Actors_Movies { get; set; }
}

} [Views] [Create.cshtml] @model Actor @{ ViewData["Title"] = "Add a new Actor"; }

Add Actor

@section Scripts{

} [Index.cshtml] @Define model@ @model IEnumerable

@Define ViewData@

@{ ViewData["Title"] ="List of Actors";

} @Program Code Starts Here -HTML@

@*@if (User.Identity.IsAuthenticated && User.IsInRole("Admin")) { }*@ @foreach (var item in Model) { @*@if (User.Identity.IsAuthenticated && User.IsInRole("Admin"))*@ @*{*@ @*@}*@ }
@Html.DisplayNameFor(model => model.ProfilePictureURL) @Html.DisplayNameFor(model => model.FullName) @Html.DisplayNameFor(model => model.Bio)Actions
@item.FullName @Html.DisplayFor(modelItem => item.FullName) @Html.DisplayFor(modelItem => item.Bio) Edit | Details | Delete

[Controllers]

using eTickets.Data; using eTickets.Data.Services; using eTickets.Models; 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()
    {
        var data = await _service.GetAll();
        return View(data);

    }
    //Get: Actors/Create
    public IActionResult Create()
    {
        return View();
    }

    //Post

    [HttpPost]
    public async Task<IActionResult> Create([Bind("FullName,ProfilePictureURL,Bio")]Actor actor)
    {
        if (!ModelState.IsValid)
        { 
            return View(actor);
        }
        _service.Add(actor);
        return RedirectToAction(nameof(Index));
    }
}

}

dwarwick commented 1 year ago

From what I see, it looks like you have covered everything. If you set a breakpoint in the controller, is the model state valid when everything is filled out correctly, and invalid when it's not?

Sincerely,

David Warwick

On Wed, May 17, 2023, 6:41 AM James Omego Marcus @.***> wrote:

Thanks @dwarwick https://github.com/dwarwick

These are my files: [Models] using System.ComponentModel.DataAnnotations;

namespace eTickets.Models { public class Actor { [Key] public int Id { get; set; }

[Display(Name ="Profile Picture")]
[Required(ErrorMessage = "Profile Picture is Required")]
public string ProfilePictureURL { get; set; }

[Display(Name = "Full Name")]
[Required(ErrorMessage = "Full Name is Required")]
[StringLength(50,MinimumLength =3,ErrorMessage ="Full Name must be between 3 and 50 Characters")]
public string FullName { get; set; }

[Display(Name = "Biography")]
[Required(ErrorMessage = "Biography is Required")]
public string Bio { get; set; }

//DB Relationship

public List<Actor_Movie> Actors_Movies { get; set; }

}

} [Views] [Create.cshtml] @model https://github.com/model Actor @{ ViewData["Title"] = "Add a new Actor"; }

Add Actor

<div class="row">
    <div class="col-md-8 offset-2">

        <form asp-action="Create"method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="form-group text-center">
                <img id="ProfilePicturePreview" />

            </div>
            <div class="form-group">
                <label asp-for="ProfilePictureURL" class="control-label"></label>
                <input asp-for="ProfilePictureURL" class="form-control" />
                <span asp-validation-for="ProfilePictureURL" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="FullName" class="control-label"></label>
                <input asp-for="FullName" class="form-control" />
                <span asp-validation-for="FullName" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Bio" class="control-label"></label>
                <input asp-for="Bio" class="form-control" />
                <span asp-validation-for="Bio" class="text-danger"></span>
            </div>

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-outline-success  float-right" style="margin-top: 15px"/>
                <a class="btn btn-outline-secondary float-end" style="margin-top: 15px" asp-action="Index">Show All</a>
            </div>
        </form>
    </div>
</div>

@section https://github.com/section Scripts{

} [Index.cshtml] @Define model@ @model https://github.com/model IEnumerable

@Define ViewData@

@{ ViewData["Title"] ="List of Actors";

} @Program Code Starts Here -HTML@ @@. https://github.com/if (User.Identity.IsAuthenticated && User.IsInRole("Admin")) { }@ @foreach https://github.com/foreach (var item in Model) { @**@.** https://github.com/if (User.Identity.IsAuthenticated && User.IsInRole("Admin"))@ @{@ @@}@

            </tr>
        }
    </tbody>

</table>

Add Actor [Controllers]

using eTickets.Data; using eTickets.Data.Services; using eTickets.Models; 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()
{
    var data = await _service.GetAll();
    return View(data);

}
//Get: Actors/Create
public IActionResult Create()
{
    return View();
}

//Post

[HttpPost]
public async Task<IActionResult> Create([Bind("FullName,ProfilePictureURL,Bio")]Actor actor)
{
    if (!ModelState.IsValid)
    {
        return View(actor);
    }
    _service.Add(actor);
    return RedirectToAction(nameof(Index));
}

}

} @Html.DisplayNameFor(model => model.ProfilePictureURL) @Html.DisplayNameFor(model => model.FullName) @Html.DisplayNameFor(model => model.Bio)Actions [image: @item.FullName] @.***> @Html.DisplayFor(modelItem => item.FullName) @Html.DisplayFor(modelItem => item.Bio) Edit | Details | Delete

— Reply to this email directly, view it on GitHub https://github.com/etrupja/complete-ecommerce-aspnet-mvc-application/issues/8#issuecomment-1551422358, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADZ27ZDP5RTKAT3ORTN7HTTXGTIRTANCNFSM6AAAAAAVAXKMVI . You are receiving this because you were mentioned.Message ID: <etrupja/complete-ecommerce-aspnet-mvc-application/issues/8/1551422358@ github.com>

JamesOmego commented 1 year ago

@dwarwick , Thank you for your assistance.

I finally figured it out. It works just fine.

dwarwick commented 1 year ago

Sounds good. Glad you got it working.

Sincerely,

David Warwick 951-210-8209

On Wed, May 17, 2023, 3:02 PM James Omego Marcus @.***> wrote:

@dwarwick https://github.com/dwarwick , Thank you for your assistance.

I finally figured it out. It works just fine.

— Reply to this email directly, view it on GitHub https://github.com/etrupja/complete-ecommerce-aspnet-mvc-application/issues/8#issuecomment-1552146450, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADZ27ZDMUZOBKYIS3J7LR43XGVDIDANCNFSM6AAAAAAVAXKMVI . You are receiving this because you were mentioned.Message ID: <etrupja/complete-ecommerce-aspnet-mvc-application/issues/8/1552146450@ github.com>

etrupja commented 1 year ago

Thank you everyone!