【问题标题】:Asp.net MVC Ajax Post - Looks for View Instead of Controller ActionAsp.net MVC Ajax Post - 寻找视图而不是控制器动作
【发布时间】:2015-12-30 17:20:49
【问题描述】:

我正在使用 AJAX 将用户选择从下拉列表中发布回我的控制器中的操作结果,该操作结果将返回部分视图。这工作正常。但是,我无法确定我更改了什么,现在它失败并出现 500 错误:

未找到视图“Create_Item_Fields_NoForm”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置: ~/Views/Request/Create_Item_Fields_NoForm.aspx ~/Views/Request/Create_Item_Fields_NoForm.ascx ~/Views/Shared/Create_Item_Fields_NoForm.aspx ~/Views/Shared/Create_Item_Fields_NoForm.ascx ~/Views/Request/Create_Item_Fields_NoForm.cshtml ~/Views/Request/Create_Item_Fields_NoForm.vbhtml ~/Views/Shared/Create_Item_Fields_NoForm.cshtml ~/Views/Shared/Create_Item_Fields_NoForm.vbhtml

为什么它要寻找视图而不是控制器动作?

我的观点

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form" }))

HTMLDropDownListFor & 部分视图的 Div

<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
    <div class="panel-body">
        <div class="form-group">
            @Html.LabelFor(model => model.itemtypes, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.DropDownListFor(model => model.itemtype, (SelectList)Model.myCollection, "Select Type", new { @id = "dropchange", @class = "form-control" })
            @Html.ValidationMessageFor(model => model.itemtypes, "", new { @class = "text-danger" })
        </div>
    <div id="itemcreate">
</div>

AJAX 发布

<script>
    $(document).ready(function () {
        $('#dropchange').change(function (e) {
            e.preventDefault();
            var data = $('form').serializeArray();
            $.ajax({
                //contentType: 'application/json; charset=utf-8',
                type: 'POST',
                url: '@Url.Action("Create_Item_Fields_NoForm", "Request")',
                data: data
            }).done(function (result) {
                $("#itemcreate").html(result)
            })
            .fail(function (jqXHR, textStatus, errorThrown) { alert(jqXHR.status, textStatus.toString, errorThrown.toString); });
        });
    });
</script>

控制器动作结果

[HttpPost]
public ActionResult Create_Item_Fields_NoForm (vmRequestCreate viewmodel)
{
    if (Request.IsAjaxRequest() && ModelState.IsValid)
    {   
        if (viewmodel.itemtype.Equals("One"))
        {
            return PartialView("_OneCreate");
        }
        else if (viewmodel.extractype.ToString() == "Two")
        {
            return PartialView("_TwoCreate");
        }
    }
    return View();
}

【问题讨论】:

  • ModelState 无效时,它将命中return View(); 行。因此它将尝试返回一个名为 Create_Item_Fields_NoForm 的视图(即与您的方法同名,因为您没有指定视图名称)。我假设您没有具有该名称的视图
  • ViewEngine 正在寻找带有 vbhtml 扩展名的视图。你是从 VB 改成 C# 的吗?
  • @Stephen Muecke 谢谢!我一定是移动了断点,因此我认为它永远不会到达控制器。如何将您的评论标记为答案?
  • @Win No...有趣的观察结果

标签: ajax asp.net-mvc partial-views


【解决方案1】:

您的vmRequestCreate 视图模型无效,因此您点击了

return View();

POST 方法中的代码行。因为您没有指定视图名称,所以它将默认使用与控制器方法同名的视图,即 Create_Item_Fields_NoForm.cshtmlwhich 不存在,因此会出现错误。更改代码以返回存在的视图名称(或为Create_Item_Fields_NoForm.cshtml 创建视图)

return View("yourViewName");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2016-05-31
    • 2019-12-17
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多