i have problem with @html.actionlink not working in mvc

0

I have a problem with function @html.actionlink and here is my sample code and the error

error:

Microsoft.Hosting.Lifetime: Information: Application started. Press Ctrl+C to shut down.
Microsoft.Hosting.Lifetime: Information: Hosting environment: Development
Microsoft.Hosting.Lifetime: Information: Content root path: C:\Users\turki\source\repos\Vidly\Vidly
The program '[1084] iisexpress.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Random view code

@model Vidly.ViewModel.RandomMovieViewModel
@{
    ViewData["Title"] = "Random";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@*
    this is comment :)
*@

<h1>Customers</h1>

<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th scope="col">Customer</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var customer in Model.Customer)
        {
            <tr class ="d-sm-table-row">
                <td>@Html.ActionLink(@customer.Name,"CustomerPage","Movie",null,null)</td>
            </tr>
        }
    </tbody>
</table>

Movie controller code

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Vidly.Models;
using Vidly.ViewModel;

namespace Vidly.Controllers
{
    public class MovieController : Controller
    {
        public IActionResult Random()
        {
            var movie = new Movie() { Name = "See" };
            var customers = new List<Customer>
            {
                new Customer {Name = "turki" },
                new Customer {Name = "ahmed" }
            };
            var ViewModel = new RandomMovieViewModel()
            {
                movie = movie,
                Customer = customers
            };
            return View(ViewModel);
        }
       
        public ActionResult CustomerPage()
        {
            return CustomerPage();
        }
    }
}
c#
asp.net-mvc
html-helper
asked on Stack Overflow Feb 12, 2021 by turki • edited Feb 12, 2021 by GSerg

1 Answer

2

You have a recursion error:

 public ActionResult CustomerPage()
        {
            return CustomerPage();
        }

The action calls itself non-stop till the exception happens. And it is not clear from your question what do you want to return as a customer page.

It should be something like this:


public ActionResult CustomerPage()
  {
var model = ....your code
 return View(model);
 }

answered on Stack Overflow Feb 12, 2021 by Sergey • edited Feb 12, 2021 by Sergey

User contributions licensed under CC BY-SA 3.0