Asp.net mvc 3 MVC 3模型绑定,未为此对象定义无参数构造函数

Asp.net mvc 3 MVC 3模型绑定,未为此对象定义无参数构造函数,asp.net-mvc-3,cascadingdropdown,Asp.net Mvc 3,Cascadingdropdown,我创建了一个工作非常出色的视图,直到我添加了一些JQuery来支持级联下拉列表。我相信这样做,我打破了视图和模型之间的束缚。提交表单时,我收到错误“没有为此对象定义无参数构造函数”。显而易见的解决方案是添加一个无参数构造函数,但我假设postmodel为null?下面是代码片段 提前感谢你的帮助 视图: 遥控器: [HttpPost] public ActionResult Create(VendorCreateModel postModel) { Vendo

我创建了一个工作非常出色的视图,直到我添加了一些JQuery来支持级联下拉列表。我相信这样做,我打破了视图和模型之间的束缚。提交表单时,我收到错误“没有为此对象定义无参数构造函数”。显而易见的解决方案是添加一个无参数构造函数,但我假设postmodel为null?下面是代码片段

提前感谢你的帮助

视图:

遥控器:

 [HttpPost]
     public ActionResult Create(VendorCreateModel postModel)
    {
        VendorCreateEditPostValidator createValidator = new VendorCreateEditPostValidator(
            postModel.iCategoryID,
            postModel.iSubCategoryID,
            postModel.AppliedPrograms,
            m_unitOfWork.ProgramRepository,
            new ModelStateValidationWrapper(ModelState));

        if (ModelState.IsValid)
        {
            int categoryId = int.Parse(postModel.iCategoryID);
            int subcategoryId = int.Parse(postModel.iSubCategoryID);
            var programIds = postModel.AppliedPrograms.Select(ap => int.Parse(ap));
            var programs = m_unitOfWork.ProgramRepository.GetPrograms(programIds);

            Vendor vendor = postModel.Vendor;
            vendor.Category = m_unitOfWork.CategoryRepository.GetCategory(categoryId);
            vendor.SubCategory = m_unitOfWork.SubCategoryRepository.GetSubCategory(subcategoryId);

            foreach (Program p in programs)
                vendor.Programs.Add(p);

            m_unitOfWork.VendorRepository.Add(vendor);
            m_unitOfWork.SaveChanges();

            return RedirectToAction("Index");  
        }

        VendorCreateModel model = new VendorCreateModel(
            postModel.Vendor,
            postModel.iCategoryID,
            postModel.iSubCategoryID,
            postModel.AppliedPrograms,
            User.Identity.Name,
            m_unitOfWork.CategoryRepository,
            m_unitOfWork.SubCategoryRepository,
            m_unitOfWork.PermissionRepository);

        return View(model);
    }
 [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult SubCategoriesByCateogry(int id)
    {
        System.Diagnostics.Debug.WriteLine(id);

        var SubCategories = db.SubCategories
            .Where(v => v.iCategoryID == id)
            .OrderBy(v => v.sDesc)
            .ToList();

        var modelData = SubCategories.Select(v => new SelectListItem()
        {
            Text = v.sDesc,
            Value = v.iSubCategoryID.ToString()
        });

        return Json(modelData, JsonRequestBehavior.AllowGet);
    }
VendorCreateModel:

public class VendorCreateModel
{
    public VendorCreateModel()
    {

    }

    public VendorCreateModel(
        Vendor vendor, 
        string categoryId,
        string subcategoryId,
        IEnumerable<string> appliedPrograms, 
        string username,
        ICategoryRepository categoryRepository,
        ISubCategoryRepository subcategoryRepository,
        IPermissionRepository permissionRepository)
    {
        UserHasProgramsValidator programValidator = new UserHasProgramsValidator(username, permissionRepository);
        var availablePrograms = programValidator.AvailablePrograms;

        HashSet<Category> applicableCategories = new HashSet<Category>();
        foreach (var p in availablePrograms)
            foreach (var c in categoryRepository.GetCategoriesByProgram(p.iProgramID))
                applicableCategories.Add(c);

        this.Vendor = vendor;
        this.AppliedPrograms = appliedPrograms;
        this.Categories = new SelectList(applicableCategories.OrderBy(x => x.sDesc).ToList(), "iCategoryID", "sDesc");
        this.SubCategories = new SelectList(subcategoryRepository.GetAllSubCategories().OrderBy(x => x.sDesc).ToList(), "iSubCategoryID", "sDesc");

        if (!string.IsNullOrEmpty(categoryId))
        {
            int temp;
            if (!int.TryParse(categoryId, out temp))
                throw new ApplicationException("Invalid Category Identifier.");
        }

        this.iCategoryID = categoryId;
        this.iSubCategoryID = subcategoryId;
        this.ProgramItems = availablePrograms
            .Select(p => new SelectListItem()
            {
                Text = p.sDesc,
                Value = p.iProgramID.ToString(),
                Selected = (AppliedPrograms != null ? AppliedPrograms.Contains(p.iProgramID.ToString()) : false)
            });
    }

    public Vendor Vendor { get; set; }
    public SelectList Categories { get; set; }
    public SelectList SubCategories { get; set; }
    public string iCategoryID { get; set; }
    public string iSubCategoryID { get; set; }
    public IEnumerable<SelectListItem> ProgramItems { get; set; }

    [AtLeastOneElementExists(ErrorMessage = "Please select at least one program.")]
    public IEnumerable<string> AppliedPrograms { get; set; }
}
公共类供应商创建模型
{
公共供应商创建模型()
{
}
公共供应商创建模型(
供应商,
字符串类别ID,
字符串子类别ID,
i可数应用程序,
字符串用户名,
ICategoryRepository类别Repository,
ISubCategoryRepository子类别存储库,
IPermissionRepository许可证(存储库)
{
UserHasProgramsValidator programValidator=新的UserHasProgramsValidator(用户名、权限存储库);
var availablePrograms=programValidator.availablePrograms;
HashSet applicatablecategories=新HashSet();
foreach(可用程序中的var p)
foreach(categoryRepository.GetCategoriesByProgram(p.iProgramID))中的变量c)
适用类别。添加(c);
这个。供应商=供应商;
this.AppliedPrograms=AppliedPrograms;
this.Categories=new SelectList(applicatablecategories.OrderBy(x=>x.sDesc.ToList(),“iCategoryID”,“sDesc”);
this.SubCategories=new SelectList(subcategory repository.GetAllSubCategories().OrderBy(x=>x.sDesc.ToList(),“isubcategoriid”,“sDesc”);
如果(!string.IsNullOrEmpty(categoryId))
{
内部温度;
如果(!int.TryParse(类别ID,输出温度))
抛出新的ApplicationException(“无效的类别标识符”);
}
this.iCategoryID=categoryId;
this.iSubCategoryID=子类别ID;
this.ProgramItems=可用程序
.Select(p=>newselectListItem()
{
Text=p.sDesc,
Value=p.iProgramID.ToString(),
Selected=(AppliedPrograms!=null?AppliedPrograms.Contains(p.iProgramID.ToString()):false)
});
}
公共供应商{get;set;}
公共选择列表类别{get;set;}
公共选择列表子类别{get;set;}
公共字符串iCategoryID{get;set;}
公共字符串IsSubcategoryId{get;set;}
公共IEnumerable程序项{get;set;}
[AtlastonElementExists(ErrorMessage=“请至少选择一个程序。”)]
公共IEnumerable应用程序{get;set;}
}

假设问题出在VendorCreateModel中,您需要添加或删除无参数构造函数,并在action方法中创建一个实例,然后使用TryUpdateModel填充它。或者使用FormsCollection(而不是fan)解析表单


您没有在此处发布viewmodel的代码,但基本假设是它将映射。

假设问题出现在您的Vendor CreateModel中,您需要添加或删除无参数构造函数,并在action方法中创建一个实例,然后通过TryUpdateModel填充它。或者使用FormsCollection(而不是fan)解析表单


您没有在此处发布viewmodel的代码,但基本假设是它将映射。

我纠正了这个问题,并希望与大家分享,以防其他人像我一样把头撞到他们的办公桌上。基本上,我更改了DropDownList for以反映:

@Html.DropDownListFor(model => model.iSubCategoryID, new SelectList(Enumerable.Empty<SelectListItem>(), "iSubCategoryID", "SubCategory",Model.SubCategories), "--Select SubCategory--", new Dictionary<string,object>{ {"class","dropdowns"},{"id","ddlSubCategory"},{"name","iSubCategoryID"}})
@Html.DropDownListFor(model=>model.iSubCategoryID,新选择列表(Enumerable.Empty(),“iSubCategoryID”,“SubCategory”,“model.SubCategories”,“model.SubCategories”),“--Select SubCategory--”,新字典{{“class”,“dropdowns”},{“id”,“ddlsubcategority”},{“name”,“iSubCategoryID”})

我纠正了这个问题,并想与大家分享一下,以防有人像我一样把头撞在桌子上。基本上,我更改了DropDownList for以反映:

@Html.DropDownListFor(model => model.iSubCategoryID, new SelectList(Enumerable.Empty<SelectListItem>(), "iSubCategoryID", "SubCategory",Model.SubCategories), "--Select SubCategory--", new Dictionary<string,object>{ {"class","dropdowns"},{"id","ddlSubCategory"},{"name","iSubCategoryID"}})
@Html.DropDownListFor(model=>model.iSubCategoryID,新选择列表(Enumerable.Empty(),“iSubCategoryID”,“SubCategory”,“model.SubCategories”,“model.SubCategories”),“--Select SubCategory--”,新字典{{“class”,“dropdowns”},{“id”,“ddlsubcategority”},{“name”,“iSubCategoryID”})

感谢您的回复。我已经用VendorCreateModel更新了我的原始帖子。我不太确定我是否完全理解你的帖子,但我正在调查。我发现了这个问题,并想与大家分享一下,以防有人像我一样把头撞在他们的桌子上。我更改了dropdownlistfor以反映:@Html.dropdownlistfor(model=>model.iSubCategoryID,new SelectList(Enumerable.Empty(),“iSubCategoryID”,“SubCategory”,“SubCategory”,“SubCategory”,“model.SubCategories”,“model.SubCategories”),“--Select SubCategory--”,new Dictionary{{{“class”,“dropdowns”},{“id”,“ddlsubcategority”},感谢您的回复。我已经用VendorCreateModel更新了我的原始帖子。我不太确定我是否完全理解你的帖子,但我正在调查。我发现了这个问题,并想与大家分享一下,以防有人像我一样把头撞在他们的桌子上。我更改了dropdownlistfor以反映:@Html.dropdownlistfor(model=>model.iSubCategoryID,new SelectList(Enumerable.Empty(),“iSubCategoryID”,“SubCategory”,“SubCategory”,“model.SubCategories”,“model.SubCategories”),“--Select SubCategory--”,new Dictionary{{{{“class”,“dropdowns”},{“id”,“ddlsubcategoryId”},{“name”,“iSubCategoryID”})
@Html.DropDownListFor(model => model.iSubCategoryID, new SelectList(Enumerable.Empty<SelectListItem>(), "iSubCategoryID", "SubCategory",Model.SubCategories), "--Select SubCategory--", new Dictionary<string,object>{ {"class","dropdowns"},{"id","ddlSubCategory"},{"name","iSubCategoryID"}})