public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ProjectNewViewModel model = (ProjectNewViewModel)bindingContext.Model ?? (ProjectNewViewModel)DependencyResolver.Current.GetService(typeof(ProjectNewViewModel)); bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName); string searchPrefix = (hasPrefix) ? bindingContext.ModelName + ".":""; //since viewmodel contains custom types like project make sure project is not null and to pass key arround for value providers //use Project.Name even if your makrup dont have Project prefix model.Project = new Project(); //populate the fields of the model model.Project.ProjectId = 0; model.Project.Name = GetValue(bindingContext, searchPrefix, "Project.Name"); model.Project.Url = GetValue(bindingContext, searchPrefix, "Project.Url"); model.Project.CreatedOn = DateTime.Now; model.Project.UpdatedOn = DateTime.Now; model.Project.isDisabled = GetCheckedValue(bindingContext, searchPrefix, "Project.isDisabled"); model.Project.isFeatured = GetCheckedValue(bindingContext, searchPrefix, "Project.isFeatured"); model.Project.GroupId = int.Parse(GetValue(bindingContext, searchPrefix, "Project.GroupId")); model.Project.Tags = new List<Tag>(); foreach (var tagid in GetValue(bindingContext, searchPrefix, "Tags").Split(',')) { var tag = new Tag { TagId = int.Parse(tagid)}; model.Project.Tags.Add(tag); } var total = model.Project.Tags.Count; return model; }
public ActionResult Create(Tag tag) { if (ModelState.IsValid) { tagRepository.InsertOrUpdate(tag); tagRepository.Save(); return RedirectToAction("Index"); } else { return View(); } }