0

I am new in asp.net-MVC and it's my first MVC app.

I have a controller linked to a partial view : (_UtilisateurController.cs) :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc_LanceurBatch.BLL;
using Mvc_LanceurBatch.DTO;
using System.ComponentModel.DataAnnotations;
using System.Web.ModelBinding;


namespace Mvc_LanceurBatch.Controllers
{
    public class _UtilisateurController : Controller
    {
        LanceurBatchDLL _oLanceurBatchDLL = new LanceurBatchDLL();

        public ActionResult Utilisateur()
        {
            
            List<UtilisateurDTO> oListUtilisateur = _oLanceurBatchDLL.GetUtilisateur("", "").ToList();

            return View(oListUtilisateur);
        }       
    }
}

Partial view (_Utilisateur.cshtml) :

@using Mvc_LanceurBatch.D TO
@model Mvc_LanceurBatch.DTO.UtilisateurDTO

    <div class="col-md-10">
        @Html.DropDownListFor(u => u.LI_LOGIN, new SelectList(Model.Utilisateurs, "LI_LOGIN", "NOM"), "Sélectionner un utilisateur")
        <input id="ButtonReinit" type="Button" value="Réinitialiser" />
    </div>
<script>
    $(document).ready(function () {
        
        $("#LI_LOGIN").change(function (e) {
            $.ajax({
                type: 'get',
                url: '@Url.Action("RechercheDemande")',
                contentType: 'application/json; charset=utf-8',
                dataType: 'html',
                data: { "Login": $(this).val() },
                success: function (result) {
                    $("#divDemande").html(result);
                },
                error: function (ex) {
                    alert("Error");
                }
            });

        });

    });
</script>

And my main view (LanceurBatch.cshtml) :

@{
    ViewBag.Title = "Lanceur de batch";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

@*@@using (Html.BeginForm("RechercheDemande", "LanceurBatch", FormMethod.Post))*@
@using (Ajax.BeginForm("RechercheDemande", "LanceurBatch", new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "POST",
    UpdateTargetId = "divPartial",
}))
{

            <div class="col-md-10">

                @Html.Action("Utilisateur", "Mvc_LanceurBatch.Controllers._UtilisateurController")
               
                <br/>
                <br />
                <div class="col-lg-12 col-md-12 col-xs-12" id="divPartial">
                    <label></label>
               
                     @Html.Action("RechercheDemande")
                </div>
            </div>
}

When I run the app, my main view (LanceurBatch.cshtml) fails at the line: @Html.Action("Utilisateur",...

The error is:

The controller for the path « /LanceurBatch/LanceurBatch » cannot be found or doesn't implement IController.

Can someone tell me what's wrong?

5
  • 1
    Why do you prefix your controller with an underscore? Commented Nov 6, 2020 at 16:56
  • ASP.NET MVC uses conventions to find things. So, by default, if given a URL like /Home/Index it looks for a controller named HomeController and a method on that controller named Index. When using Html.Action, it does a similar lookup strategy. Commented Nov 6, 2020 at 17:03
  • gunr2171 : I have prefixed it with underscore because it's a partial view. Heretic Monkey : I tried every path for the html.Action but none has succeeded. Commented Nov 6, 2020 at 22:05
  • Don't use the underscore there. Take a look at coding conventions from Microsoft: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… Commented Nov 8, 2020 at 10:21
  • Paul : thank you for your link. But despite the conventions names, it still doesn't work. Commented Nov 9, 2020 at 17:59

1 Answer 1

1

I have found what's wrong. I have renamed the view and the controller without underscores. In the view LanceurBatch.cshtml I have modified :

@Html.Action("Utilisateur", "Utilisateur")

In the controller UtilisateurController.cs :

return PartialView("~/Views/LanceurBatch/Utilisateur.cshtml", oUtilisateur);

And I have removed from the LanceurBatch.cshtml:

@Html.Action("RechercheDemande")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.