Asp.net MVC Ajax Post - 查找视图而不是控制器操作
Asp.net MVC Ajax Post - Looks for View Instead of Controller Action
我正在使用 AJAX 来 post 用户从下拉列表中选择返回到我的控制器中的操作结果,这将 return 局部视图。这工作正常。但是,我无法确定我更改了什么,现在它因 500 错误而失败:
The view 'Create_Item_Fields_NoForm' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/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 Post
<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();
}
您的 vmRequestCreate
视图模型无效,因此您点击了
return View();
您的 POST 方法中的代码行。因为您没有指定视图名称,它将默认使用与控制器方法同名的视图,即 Create_Item_Fields_NoForm.cshtml
不存在,因此会出现错误。将代码更改为 return 存在的视图名称(或为 Create_Item_Fields_NoForm.cshtml
创建视图)
return View("yourViewName");
我正在使用 AJAX 来 post 用户从下拉列表中选择返回到我的控制器中的操作结果,这将 return 局部视图。这工作正常。但是,我无法确定我更改了什么,现在它因 500 错误而失败:
The view 'Create_Item_Fields_NoForm' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/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 Post
<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();
}
您的 vmRequestCreate
视图模型无效,因此您点击了
return View();
您的 POST 方法中的代码行。因为您没有指定视图名称,它将默认使用与控制器方法同名的视图,即 Create_Item_Fields_NoForm.cshtml
不存在,因此会出现错误。将代码更改为 return 存在的视图名称(或为 Create_Item_Fields_NoForm.cshtml
创建视图)
return View("yourViewName");