1

I'm having problems submitting a form via AJAX to my controller. Other people with similar error message seem to have issues around using interfaces as types on their controller method stub, I don't have that issue. I've included my partial view and controller code, if I missed any information out just let me know. I suspect it has something to do with the creation of the model after I change the value in the drop down list, but the error message isn't the most helpful so I'm a bit stuck!

My Partial View:

<% using (Ajax.BeginForm("RequestDetails", "Home", new { RequestId = Model.Request.RequestId, RequestStatus = Model.Request.Status }, new AjaxOptions
        {
            HttpMethod = "POST",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "Details"
        },
        new { @class = "jqtransform" }))
        {%>

        <%= Html.LabelFor(m => Model.Request.Name) %>
        <p><%= Html.Encode(Model.Request.Name)%>&nbsp;</p>
        <%= Html.LabelFor(m => Model.Request.Status) %>
        <% if (Html.IsAdmin(this.Page))
        {%>
            <div class="rowElem">
            <%=Html.DropDownListFor(m => Model.Request.Status, (IEnumerable<SelectListItem>)ViewData["requestStatus"], null, new { onchange = "this.form.submit();" })%>
            </div>
    <%  }
        else
        { %>
            <p><%= Html.Encode(Model.Request.Status.GetStringValue())%>&nbsp;</p>
        <% } }%>    

My Controller:

[HttpPost]
    public PartialViewResult RequestDetails(RequestDetailModel model, Guid RequestId, string RequestStatus)
    {
        if (ModelState.IsValid)
        {
            RequestMethods requestMethods = new RequestMethods();

            IRequest request = _requestRepo.GetRequestById(CurrentUser, RequestId, CompanyId);

            requestMethods.UpdateRequestStatus(request, CurrentUser, model.NewComment, model.Request.Status);

        }

        model.Request = _requestRepo.GetRequestById(CurrentUser, RequestId, CompanyId);

        return PartialView(model);
    }

The exact error thrown is:

Cannot create an instance of an interface.

at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) at System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) at System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

Mark
  • 816
  • 1
  • 9
  • 27

1 Answers1

2

You haven't shown your RequestDetailModel object but if there are properties that are interface type the default model binder is not able to populate them. For example if you have the following:

public class RequestDetailModel 
{
    public IRequest Request { get; set; }
}

where IRequest is an interface or an abstract class this won't work. If you want to use interfaces in your models you will need to write a custom model binder. Here's one example of such custom model binder that works with abstract classes but interfaces would be the same.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • You were right, instead of using an interface I fully fleshed out my model to get access to all the properties I needed. Thanks! – Mark Apr 26 '12 at 16:06