I am using AJAX to post a user selection from a dropdown back to an actionresult in my controller that will return a partial view. This was working correctly. However, I cant identify what I changed and now it fails with a 500 error:
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
Why is it looking for a view instead of a controller action?
Top of my view
@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 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>
Controller 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();
}
ModelState
is not valid, it will hit thereturn View();
line. So it will attempt to return a view namedCreate_Item_Fields_NoForm
(i.e. the same name as your method because you have not specified a view name). I assume you do not have a view with that name – user3559349