2013-04-05 60 views
1

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

在此先感谢您的帮助。

查看:

<script type="text/javascript"> 
$(document).ready(function() { 
     $("#ddlCategories").change(function() { 
     var iCategoryId = $(this).val(); 
      $.getJSON(
       "@Url.Content("~/Remote/SubCategoriesByCateogry")", 
       { id: iCategoryId }, 
       function (data) { 
        var select = ResetAndReturnSubCategoryDDL(); 
        $.each(data, function (index, itemData) { 
         select.append($('<option/>', { value: itemData.Value, text: itemData.Text })); 
        }); 
       }); 
     }); 
    function ResetAndReturnSubCategoryDDL() { 
     var select = $('#ddlSubCategory'); 
     select.empty(); 
     select.append($('<option/>', { value: '', text: "--Select SubCategory--" })); 
     return select; 
    } 
}); 

...

<div class="editor-field"> 
      @Html.DropDownList("iCategoryID", Model.Categories,"--Select Category--", new Dictionary<string,object>{ {"class","dropdowns"},{"id","ddlCategories"}}) 
      @Html.ValidationMessage("iCategoryID") 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.SubCategories, "SubCategory") 
     </div> 
     <div class="editor-field"> 
       @Html.DropDownListFor(model => model.SubCategories, new SelectList(Enumerable.Empty<SelectListItem>(), "iSubCategoryID", "SubCategory",Model.SubCategories), "--Select SubCategory--", new { id = "ddlSubCategory" }) 
       @Html.ValidationMessage("iSubCategoryID") 
     </div> 

控制器:

[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); 
    } 

RemoteController:

[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; } 
} 

回答

1

我纠正了这个问题,并希望分享以防其他人像我一直在他们的桌子上撞头。基本上我改变了dropdownlistfor反映:

@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"}}) 
0

假设这里的问题是在你的VendorCreateModel,您可能需要通过TryUpdateModel添加一个参数的构造函数或将其删除,并在你的操作方法创建一个实例并填充它。或者使用FormsCollection解析表单(不是粉丝)。

你没有你的viewmodel的代码发布在这里,但基本的假设是它将映射。

+0

感谢您的答复亚当。我已使用VendorCreateModel更新了原始帖子。我不太确定我是否完全理解你的帖子,但正在研究它。 – 2013-04-08 11:52:32

+0

找出问题并想要分享,以防别人像我一样在他们的桌子上砸头。我改变了下拉列表以反映: @ Html.DropDownListFor(model => model.iSubCategoryID,new SelectList(Enumerable.Empty (),“iSubCategoryID”,“SubCategory”,Model.SubCategories),“--Select SubCategory--“,新字典 {{”class“,”dropdowns“},{”id“,”ddlSubCategory“},{”name“,”iSubCategoryID“}}) – 2013-04-08 13:35:00

相关问题