【问题标题】:Why Html.Action fails while not finding controller?为什么 Html.Action 在找不到控制器时失败?
【发布时间】:2020-11-06 16:55:35
【问题描述】:

我是 asp.net-MVC 的新手,这是我的第一个 MVC 应用程序。

我有一个链接到局部视图的控制器:(_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);
        }       
    }
}

部分视图(_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>

还有我的主要观点(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>
}

当我运行应用程序时,我的主视图 (LanceurBatch.cshtml) 在以下行失败:@Html.Action("Utilisateur",...

错误是:

找不到路径 « /LanceurBatch/LanceurBatch » 的控制器或未实现 IController。

谁能告诉我怎么了?

【问题讨论】:

  • 为什么要在控制器前加上下划线?
  • ASP.NET MVC 使用约定来查找内容。因此,默认情况下,如果给定一个像/Home/Index 这样的URL,它会查找一个名为HomeController 的控制器和一个名为Index 的控制器上的方法。当使用Html.Action 时,它会执行类似的查找策略。
  • gunr2171 :我用下划线作为前缀,因为它是部分视图。异端猴子:我尝试了 html.Action 的所有路径,但没有一个成功。
  • 不要在此处使用下划线。看看微软的编码约定:docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
  • 保罗:谢谢你的链接。但是尽管有约定名称,它仍然不起作用。

标签: c# asp.net-mvc-4 razor


【解决方案1】:

我发现了问题所在。 我已经重命名了视图和控制器,没有下划线。 在视图 LanceurBatch.cshtml 我已经修改:

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

在控制器 UtilisateurController.cs 中:

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

我已经从 LanceurBatch.cshtml 中删除了:

@Html.Action("RechercheDemande")

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 2015-07-07
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多