Reputation: 1104
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();
}
Upvotes: 1
Views: 652
Reputation:
Your vmRequestCreate
view model is not valid and as a result you hit the
return View();
line of code in your POST method. Because you do not specify a view name, it will default to using a view with the same name as the controller method, i.e. Create_Item_Fields_NoForm.cshtml
which does not exist, hence the error. Change the code to return a view name which exists (or create a view for Create_Item_Fields_NoForm.cshtml
)
return View("yourViewName");
Upvotes: 3