我正在使用AJAX将用户选择从下拉列表发回到我的控制器中的actionresult,它将返回部分视图。这工作正常。但是,我无法识别我更改的内容,现在它因500错误而失败:
视图' Create_Item_Fields_NoForm'或者找不到它的主人,或者没有视图引擎支持搜索到的位置。搜索了以下位置: 〜/查看/申请/ Create_Item_Fields_NoForm.aspx 〜/查看/申请/ Create_Item_Fields_NoForm.ascx 〜/查看/共享/ Create_Item_Fields_NoForm.aspx 〜/查看/共享/ Create_Item_Fields_NoForm.ascx 〜/查看/申请/ Create_Item_Fields_NoForm.cshtml 〜/查看/申请/ Create_Item_Fields_NoForm.vbhtml 〜/查看/共享/ Create_Item_Fields_NoForm.cshtml 〜/查看/共享/ Create_Item_Fields_NoForm.vbhtml
为什么要查找视图而不是控制器操作?
我的观点
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form" }))
HTMLDropDownListFor& Div for Partial View
<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>
控制器ActionResult
[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();
}
答案 0 :(得分:2)
您的vmRequestCreate
视图模型无效,因此您点击了
return View();
POST方法中的代码行。因为您没有指定视图名称,所以它将默认使用与控制器方法同名的视图,即不存在的Create_Item_Fields_NoForm.cshtml
,因此出错。更改代码以返回存在的视图名称(或创建Create_Item_Fields_NoForm.cshtml
的视图)
return View("yourViewName");