0

can anyone can help me finding why this is not working:

Model:

public class CreateAdCategoryViewModel
{
    [Display(ResourceType = typeof(HeelpResources), Name = "AdViewModel_Category_Label")]
    [Required(ErrorMessageResourceName = "AdViewModel_Required_ErrorMsg", ErrorMessageResourceType = typeof(HeelpResources))]
    public int Category_Id { get; set; }

    public IEnumerable<SelectListItem> CategoryList { get; set; }

    public CreateAdCategoryViewModel(IEnumerable<SelectListItem> categoryList)
    {
        CategoryList = categoryList;
    }
}

Controller:

    [Authorize]
    [HttpPost]
    public virtual PartialViewResult CreateAdCategoryType(CreateAdCategoryViewModel model)
    {
        // Build the ViewModel to return to the View with the Category Drop List
        return PartialView(new CreateAdCategoryTypeViewModel(CategoryDropDownList()));
    }

View:

@model Heelp.ViewModels.CreateAdCategoryViewModel

@using (Ajax.BeginForm(MVC.Ad.CreateAdCategoryType(), new AjaxOptions { UpdateTargetId = "category_type", InsertionMode = InsertionMode.Replace }, new { @id = "categoryForm" }))
{
    @Html.DisplayNameFor(m => m.Category_Id)
    @Html.DropDownListFor(m => m.Category_Id, Model.CategoryList, HeelpResources.DropdownlistCategoryFirstRecord)
    @Html.ValidationMessageFor(m => m.Category_Id)
}

The submit is made by Javascript:

$(document).ready(function ()
{
    $("#Category_Id").change(function ()
    {
        $("#categoryForm").submit();
    });
});

The problem here is that the submit never finds the action CreateAdCategoryType with the mode as parameter, why?

tereško
  • 58,060
  • 25
  • 98
  • 150
Patrick
  • 2,995
  • 14
  • 64
  • 125
  • May be because your model has parametrized constructor and MVC doesn't know how to call it? – T.S. Jan 03 '13 at 15:18
  • Yes your are correct, I take off the constructor and everything works fine, so how can I have a constructor to simplify the return of the model to the view using a diferent approach? – Patrick Jan 03 '13 at 18:38

1 Answers1

0

When It comes to models that go into controller, they need to be POCOs. MVC framework simply will not know how to create instance if you don't have parameter-less constructor. Remember, it needs to hydrate your model. It doesn't know how to do

new CreateAdCategoryTypeViewModel(CategoryDropDownList())

It needs to create an instance and discover (reflect) all public properties and hydrate it. in your case, even if it (framework) finds the constructor, it wouldn't know what data to supply to it. Although, I think, it only looks for parameter-less constructors.

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Thanks, so you are saying that ViewModels can never have a constructor, only public properties so they can be mapped when data arrives to the Action? – Patrick Jan 05 '13 at 12:59
  • It has to be a parameter-less constructor. Since you have declared your special constructor, it doesn't know what to do with it. Try to explicitly add parameter-less constructor. But generally, the model should be your DTO and nothing else. – T.S. Jan 07 '13 at 14:33