1
votes

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();
}
1
When ModelState is not valid, it will hit the return View(); line. So it will attempt to return a view named Create_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 nameuser3559349
ViewEngine is looking for a View with vbhtml extension. Did you change from VB to C#?Win
@Stephen Muecke Thanks! I must have moved my breakpoint and as such I thought it was never reaching the controller. How can I mark your comment as the answer?HendPro12
@Win No...interesting observation thoughHendPro12

1 Answers

3
votes

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.cshtmlwhich 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");