Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Willem Luijk 24 posts 95 karma points
    Aug 31, 2015 @ 19:03
    Willem Luijk
    0

    Postback to RenderMvcController not working...Edit

    I have to integrate some MVC 4 login in Umbraco about a thousands records table, some crud logic cant get the post working.

    This is what i made so far.

    The model:

    public class AnimalViewModel : RenderModel
    {
    
        public AnimalViewModel(IPublishedContent content, CultureInfo culture, Animal animal)
            : base(content, culture)
        {
            Animal = animal;
            Init(animal);
        }
    
        public AnimalViewModel(IPublishedContent content, CultureInfo culture)
            : base(content, culture)
        {
            Init(new Animal());
        }
    
        public Animal Animal { get; set; }
    
        public IEnumerable<SelectListItem> Genders { get; set; }
    
        public IEnumerable<SelectListItem> IsAliveStatuses { get; set; }
        public IEnumerable<SelectListItem> CommercialStatuses { get; set; }
    
        public IEnumerable<SelectListItem> Sires { get; set; }
        public Animal Sire { get; set; }
    
        public IEnumerable<SelectListItem> Dams { get; set; }
        public Animal Dam { get; set; }
    
        public IEnumerable<SelectListItem> CoatColors { get; set; }
    
        public IEnumerable<SelectListItem> Breeders { get; set; }
        public Breeder Breeder { get; set; }
    
        public IEnumerable<SelectListItem> Owners { get; set; }
        public Breeder Owner { get; set; }
    
        public IEnumerable<SelectListItem> Countries { get; set; }
        public IEnumerable<SelectListItem> YesNoFromEmbryo { get; set; }
        public IEnumerable<SelectListItem> Predicates { get; set; }
        public IEnumerable<SelectListItem> PredicatesOld { get; set; }
        public IEnumerable<SelectListItem> CarcassTypes { get; set; }
    
        public IEnumerable<SelectListItem> IBRType { get; set; }
        public IEnumerable<SelectListItem> LeptoType { get; set; }
        public IEnumerable<SelectListItem> BVDType { get; set; }
        public IEnumerable<SelectListItem> SalmonellaType { get; set; }
    
        public IEnumerable<SelectListItem> PelvisWidthMeasurements { get; set; }
        public IEnumerable<SelectListItem> ScrotalCircumferences { get; set; }
        public IEnumerable<SelectListItem> PolledStatuses { get; set; }
        public IEnumerable<SelectListItem> CaeseryeanStatuses { get; set; }
        public IEnumerable<SelectListItem> DocilityStatuses { get; set; }
        public IEnumerable<SelectListItem> LocomotionStatuses { get; set; }
        public IEnumerable<SelectListItem> TeethPrecisions { get; set; }
        public IEnumerable<SelectListItem> DoubleMuscleStatuses { get; set; }
        public IEnumerable<SelectListItem> DNAStatuses { get; set; }
        public IEnumerable<SelectListItem> StrawsAvailibilties { get; set; }
        public IEnumerable<SelectListItem> EmbryoAvailabilities { get; set; }
        public IEnumerable<SelectListItem> JohnesDiseaseHealthSchemes { get; set; }
    
        [DisplayName("Upload afbeelding")]
        public HttpPostedFileBase AnimalImageUpload { get; set; }
    
        public bool AnimalImageExist
        {
            get
            {
                return System.IO.File.Exists(AnimalImagePathAbsolute);
            }
        }
    
        public string AnimalImageViaController
        {
            get
            {
                return string.Format("/Animal/Image?Name={0}{1}", Animal.AnimalID, Animal.ImageExt);
            }
        }
    
        public string AnimalImagePath
        {
            get{
                return string.Format("~/App_Data/Uploads/{0}{1}", Animal.AnimalID, Animal.ImageExt);
            }
        }
    
        public string AnimalImagePathAbsolute
        {
            get{
                return HttpContext.Current.Server.MapPath(AnimalImagePath);
            }
        }
    
        private void Init(Animal animal)
        {
            Genders = EnumExtensions.ToSelectList<Enums.Gender>(animal.Gender);
            Countries = EnumExtensions.ToSelectList<Enums.Country>(animal.Country);
            IsAliveStatuses = EnumExtensions.ToSelectList<Enums.YesNo>(animal.IsAlive);
            CommercialStatuses = EnumExtensions.ToSelectList<Enums.CommercialStatus>(animal.CommercialStatus);
            CoatColors = EnumExtensions.ToSelectList<Enums.CoatColor>(animal.CoatColor);
            YesNoFromEmbryo = EnumExtensions.ToSelectList<Enums.YesNo>(animal.FromEmbryo);
            Predicates = EnumExtensions.ToSelectList<Enums.Predicate>(animal.Predicate);
            PredicatesOld = EnumExtensions.ToSelectList<Enums.PredicateOld>(animal.PredicateOld);
            CarcassTypes = EnumExtensions.ToSelectList<Enums.CarcassType>(animal.CarcassType);
    
            Sires = animalRepo.GetAllAsSelectListOnSexe(animal.AnimalID, animal.SireID, Enums.Gender.Male, Const.EmptyMaleID);
            Sire = animalRepo.Get(animal.SireID);
    
            Dams = animalRepo.GetAllAsSelectListOnSexe(animal.AnimalID, animal.DamID, Enums.Gender.Female, Const.EmptyFemaleID);
            Dam = animalRepo.Get(animal.DamID);
    
            Breeders = breederRepo.GetAllAsSelectList(animal.BreederID);
            Owners = breederRepo.GetAllAsSelectListWithEmpty(animal.OwnerID, Const.EmptyOwnerID);
    
            Breeder = breederRepo.Get(animal.BreederID);
            Owner = breederRepo.Get(animal.OwnerID);
    
            PelvisWidthMeasurements = EnumExtensions.ToSelectList<Enums.SufficientInsufficientUnknown>(animal.PelvisWidthMeasurement);
            ScrotalCircumferences = EnumExtensions.ToSelectList<Enums.SufficientInsufficientUnknown>(animal.ScrotalCircumference);
            PolledStatuses = EnumExtensions.ToSelectList<Enums.PolledStatus>(animal.PolledStatus);
            CaeseryeanStatuses = EnumExtensions.ToSelectList<Enums.YesNoUnknown>(animal.CaeseryeanStatus);
            DocilityStatuses = EnumExtensions.ToSelectList<Enums.SufficientInsufficientUnknown>(animal.DocilityStatus);
            LocomotionStatuses = EnumExtensions.ToSelectList<Enums.SufficientInsufficientUnknown>(animal.LocomotionStatus);
            TeethPrecisions = EnumExtensions.ToSelectList<Enums.SufficientInsufficientUnknown>(animal.TeethPrecision);
            DoubleMuscleStatuses = EnumExtensions.ToSelectList<Enums.DoubleMuscleStatus>(animal.DoubleMuscleStatus);
            DNAStatuses = EnumExtensions.ToSelectList<Enums.DNAStatus>(animal.DNAStatus);
            StrawsAvailibilties = EnumExtensions.ToSelectList<Enums.YesNo>(animal.StrawsAvailibilty);
            EmbryoAvailabilities = EnumExtensions.ToSelectList<Enums.YesNo>(animal.EmbryoAvailability);
            IBRType = EnumExtensions.ToSelectList<Enums.YesNoUnknown>(animal.IBRHealthScheme);
            LeptoType = EnumExtensions.ToSelectList<Enums.YesNoUnknown>(animal.LeptoHealthScheme);
            BVDType = EnumExtensions.ToSelectList<Enums.YesNoUnknown>(animal.BVDHealthScheme);
            SalmonellaType = EnumExtensions.ToSelectList<Enums.YesNoUnknown>(animal.SalmonellaHealthScheme);
            JohnesDiseaseHealthSchemes = EnumExtensions.ToSelectList<Enums.JohnesDiseaseHealthScheme>(animal.JohnesDiseaseHealthScheme);
        }
    
    }
    

    This all is merged from working MVC 4 to Umbraco. The controller looks like:

    public class AnimalController : RenderMvcController
    {
    
        AnimalRepo animalRepo = new AnimalRepo();
    
        //
        // GET: /Animal/
    
        public ActionResult AnimalIndex(Umbraco.Web.Models.RenderModel model, string sortOrder, string searchString)
        {
            AnimalIndexViewModel vm = new AnimalIndexViewModel(model.Content, CultureInfo.CurrentCulture);
    
            vm.animals = (from a in animalRepo.GetAll() select a);
            vm.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            vm.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            vm.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            if (!String.IsNullOrEmpty(searchString))
            {
                ViewBag.SearchString = searchString;
                vm.animals = vm.animals.Where(s => s.Name.Contains(searchString)
                                       || s.EartagCode.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    vm.animals = vm.animals.OrderByDescending(s => s.Name);
                    break;
    
                case "date":
                    vm.animals = vm.animals.OrderBy(s => s.DateOfBirth);
                    break;
    
                case "date_desc":
                    vm.animals = vm.animals.OrderByDescending(s => s.DateOfBirth);
                    break;
    
                default:
                    vm.animals = vm.animals.OrderBy(s => s.Name);
                    break;
            }
            return CurrentTemplate(vm);
        }
    
        //
        // GET: /Animal/Details/5
    
        public ActionResult AnimalDetails(Umbraco.Web.Models.RenderModel model, Guid id)
        {
            if (id == null)
            {
                RedirectToAction("AnimalIndex", "AnimalController");
            }
    
            AnimalViewModel animalViewModel = new AnimalViewModel(model.Content, CultureInfo.CurrentCulture, animalRepo.Get(id));
            return CurrentTemplate(animalViewModel);
        }
    
        [HttpGet]
        public ActionResult AnimalEdit(Umbraco.Web.Models.RenderModel model, Guid id)
        {
            Animal animal = animalRepo.Get(id);
    
            if (animal == null)
            {
                return HttpNotFound();
            }
    
            AnimalViewModel vm = new AnimalViewModel(model.Content, model.CurrentCulture, animal);
    
            return CurrentTemplate(vm);
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AnimalEdit(AnimalViewModel animalViewModel)
        {
    
            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };
    
            if (animalViewModel.AnimalImageUpload != null && animalViewModel.AnimalImageUpload.ContentLength != 0)
            {
                if (!validImageTypes.Contains(animalViewModel.AnimalImageUpload.ContentType))
                {
                    ModelState.AddModelError("AnimalImageImageUpload", "Please choose either a GIF, JPG or PNG image.");
                }
            }
    
            if (ModelState.IsValid)
            {
    
                if (animalViewModel.AnimalImageUpload != null && animalViewModel.AnimalImageUpload.ContentLength > 0)
                {
                    var uploadDir = "~/App_Data/Uploads";
                    var fileName = string.Format("{0}{1}", animalViewModel.Animal.AnimalID, System.IO.Path.GetExtension(animalViewModel.AnimalImageUpload.FileName));
                    var imagePath = Path.Combine(Server.MapPath(uploadDir), fileName);
                    var imageUrl = Path.Combine(uploadDir, fileName);
    
                    if (animalViewModel.AnimalImageExist)
                        System.IO.File.Delete(animalViewModel.AnimalImagePathAbsolute);
    
                    animalViewModel.AnimalImageUpload.SaveAs(imagePath);
                    animalViewModel.Animal.ImageExt = System.IO.Path.GetExtension(animalViewModel.AnimalImageUpload.FileName);
                }
    
                animalRepo.Edit(animalViewModel.Animal);
                return RedirectToAction("Index");
            }
    
            // return View(animalViewModel);
            return CurrentTemplate(animalViewModel);
        }
    

    And the view for the AnimalEdit is build up as next:

    @inherits UmbracoViewPage<PedigreeCMS.Models.AnimalViewModel>
    
    @{
        Layout = "Master.cshtml";
    }
    
    @{
        ViewBag.Title = "Edit Animal";
    }
    
    <h2>Edit</h2>
    
    @using (Html.BeginUmbracoForm("AnimalEdit", "AnimalController"))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
    
        <fieldset>
    
            <legend>Dier</legend>
    
            @Html.HiddenFor(model => model.Animal.AnimalID)
            @Html.HiddenFor(model => model.Animal.ImageExt)
    
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Animal.Name)
                @Html.ValidationMessageFor(model => model.Animal.Name)
            </div>
    
            @if (Model.AnimalImageExist)
            {
                <img src="@Url.Content(Model.AnimalImageViaController)" alt="Image" width="300px" />
            }
    
            <div class="editor-label">
                @Html.LabelFor(model => model.AnimalImageUpload)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.AnimalImageUpload, new { type = "file" })
                @Html.ValidationMessageFor(model => model.AnimalImageUpload)
            </div>
    
            @Html.ActionLink("Verwijder afbeelding","DeleteImage", new { AnimalID = Model.Animal.AnimalID, FileExt = Model.Animal.ImageExt })
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.Country)
            </div>
    
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.Country, Model.Countries)
                @Html.ValidationMessageFor(model => model.Animal.Country)
            </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.BreederID)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.BreederID, Model.Breeders, "Select please...")
                    @Html.ValidationMessageFor(model => model.Animal.BreederID)
                </div>
    
                <legend>Eigenaar</legend>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.OwnerID)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.OwnerID, Model.Owners)
                    @Html.ValidationMessageFor(model => model.Animal.OwnerID)
                </div>
    
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.Country)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.Country, Model.Countries)
                @Html.ValidationMessageFor(model => model.Animal.Country)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.EartagCode)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Animal.EartagCode)
                @Html.ValidationMessageFor(model => model.Animal.EartagCode)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.Gender)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.Gender, Model.Genders)
                @Html.ValidationMessageFor(model => model.Animal.Gender)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.DateOfBirth)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Animal.DateOfBirth)
                @Html.ValidationMessageFor(model => model.Animal.DateOfBirth)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.FromEmbryo)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.FromEmbryo, Model.YesNoFromEmbryo)
                @Html.ValidationMessageFor(model => model.Animal.FromEmbryo)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.BreedRating)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Animal.BreedRating)
                @Html.ValidationMessageFor(model => model.Animal.BreedRating)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.Predicate)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.Predicate, Model.Predicates, "Kies predicaat...")
                @Html.ValidationMessageFor(model => model.Animal.Predicate)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.PredicateOld)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.PredicateOld, Model.PredicatesOld, "Kies oude predicaat...")
                @Html.ValidationMessageFor(model => model.Animal.PredicateOld)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.CoatColor)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.CoatColor, Model.CoatColors)
                @Html.ValidationMessageFor(model => model.Animal.CoatColor)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.SireID)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.SireID, Model.Sires)
                @Html.ValidationMessageFor(model => model.Animal.SireID)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.DamID)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.DamID, Model.Dams)
                @Html.ValidationMessageFor(model => model.Animal.DamID)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Animal.CarcassType)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Animal.CarcassType, Model.CarcassTypes)
                @Html.ValidationMessageFor(model => model.Animal.CarcassType)
            </div>
    
            <fieldset>
    
                <legend>Exterieur keuring</legend>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DateOfExternalExamination)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DateOfExternalExamination)
                    @Html.ValidationMessageFor(model => model.Animal.DateOfExternalExamination)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Development)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Development)
                    @Html.ValidationMessageFor(model => model.Animal.Development)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DevelopmentAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DevelopmentAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.DevelopmentAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.RaceType)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.RaceType)
                    @Html.ValidationMessageFor(model => model.Animal.RaceType)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.RaceTypeAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.RaceTypeAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.RaceTypeAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Muscling)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Muscling)
                    @Html.ValidationMessageFor(model => model.Animal.Muscling)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.MusclingAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.MusclingAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.MusclingAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Bones)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Bones)
                    @Html.ValidationMessageFor(model => model.Animal.Bones)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.BonesAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.BonesAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.BonesAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.GeneralAppearance)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.GeneralAppearance)
                    @Html.ValidationMessageFor(model => model.Animal.GeneralAppearance)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.GeneralAppearanceAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.GeneralAppearanceAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.GeneralAppearanceAccuracy)
                </div>
    
            </fieldset>
            <fieldset>
    
                <legend>Genomics Analyse</legend>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DateGenomicsExamination)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DateGenomicsExamination)
                    @Html.ValidationMessageFor(model => model.Animal.DateGenomicsExamination)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.FN_CalvingEase)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.FN_CalvingEase)
                    @Html.ValidationMessageFor(model => model.Animal.FN_CalvingEase)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.FN_CalvingEaseAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.FN_CalvingEaseAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.FN_CalvingEaseAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.CR_Growth)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.CR_Growth)
                    @Html.ValidationMessageFor(model => model.Animal.CR_Growth)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.CR_GrowthAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.CR_GrowthAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.CR_GrowthAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DM_MusclingGen)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DM_MusclingGen)
                    @Html.ValidationMessageFor(model => model.Animal.DM_MusclingGen)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DM_MusclingGenAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DM_MusclingGenAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.DM_MusclingGenAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DS_SkeleticalDevelopment)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DS_SkeleticalDevelopment)
                    @Html.ValidationMessageFor(model => model.Animal.DS_SkeleticalDevelopment)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DS_SkeleticalDevelopmentAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.DS_SkeleticalDevelopmentAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.DS_SkeleticalDevelopmentAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.FOS_BoneFineness)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.FOS_BoneFineness)
                    @Html.ValidationMessageFor(model => model.Animal.FOS_BoneFineness)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.AV_CalvingAbility)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.AV_CalvingAbility)
                    @Html.ValidationMessageFor(model => model.Animal.AV_CalvingAbility)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.AV_CalvingAbilityAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.AV_CalvingAbilityAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.AV_CalvingAbilityAccuracy)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.AL_MilkAbility)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.AL_MilkAbility)
                    @Html.ValidationMessageFor(model => model.Animal.AL_MilkAbility)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.AL_MilkAbilityAccuracy)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.AL_MilkAbilityAccuracy)
                    @Html.ValidationMessageFor(model => model.Animal.AL_MilkAbilityAccuracy)
                </div>
    
            </fieldset>
            <fieldset>
    
                <legend>Overige</legend>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.BirthWeightWeighted)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.BirthWeightWeighted)
                    @Html.ValidationMessageFor(model => model.Animal.BirthWeightWeighted)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.BirthWeightEstimated)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.BirthWeightEstimated)
                    @Html.ValidationMessageFor(model => model.Animal.BirthWeightEstimated)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.CommercialStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.CommercialStatus, Model.CommercialStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.CommercialStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.IsAlive)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.IsAlive, Model.IsAliveStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.IsAlive)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Weight100Days)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Weight100Days)
                    @Html.ValidationMessageFor(model => model.Animal.Weight100Days)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Weight200Days)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Weight200Days)
                    @Html.ValidationMessageFor(model => model.Animal.Weight200Days)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Weight300Days)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Weight300Days)
                    @Html.ValidationMessageFor(model => model.Animal.Weight300Days)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Weight400Days)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Weight400Days)
                    @Html.ValidationMessageFor(model => model.Animal.Weight400Days)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.Weight500Days)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.Weight500Days)
                    @Html.ValidationMessageFor(model => model.Animal.Weight500Days)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.LongevityYears)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.LongevityYears)
                    @Html.ValidationMessageFor(model => model.Animal.LongevityYears)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.CarcassWeight)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.CarcassWeight)
                    @Html.ValidationMessageFor(model => model.Animal.CarcassWeight)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.PelvisWidthMeasurement)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.PelvisWidthMeasurement)
                    @Html.ValidationMessageFor(model => model.Animal.PelvisWidthMeasurement)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.ScrotalCircumference)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Animal.ScrotalCircumference)
                    @Html.ValidationMessageFor(model => model.Animal.ScrotalCircumference)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.PolledStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.PolledStatus, Model.PolledStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.PolledStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.CaeseryeanStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.CaeseryeanStatus, Model.CaeseryeanStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.CaeseryeanStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DocilityStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.DocilityStatus, Model.DocilityStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.DocilityStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.LocomotionStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.LocomotionStatus, Model.LocomotionStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.LocomotionStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.TeethPrecision)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.TeethPrecision, Model.TeethPrecisions)
                    @Html.ValidationMessageFor(model => model.Animal.TeethPrecision)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DoubleMuscleStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.DoubleMuscleStatus, Model.DoubleMuscleStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.DoubleMuscleStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.DNAStatus)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.DNAStatus, Model.DNAStatuses)
                    @Html.ValidationMessageFor(model => model.Animal.DNAStatus)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.StrawsAvailibilty)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.StrawsAvailibilty, Model.StrawsAvailibilties)
                    @Html.ValidationMessageFor(model => model.Animal.StrawsAvailibilty)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.EmbryoAvailability)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.EmbryoAvailability, Model.EmbryoAvailabilities)
                    @Html.ValidationMessageFor(model => model.Animal.EmbryoAvailability)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.IBRHealthScheme)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.IBRHealthScheme, Model.IBRType)
                    @Html.ValidationMessageFor(model => model.Animal.IBRHealthScheme)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.LeptoHealthScheme)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.LeptoHealthScheme, Model.LeptoType)
                    @Html.ValidationMessageFor(model => model.Animal.LeptoHealthScheme)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.BVDHealthScheme)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.BVDHealthScheme, Model.BVDType)
                    @Html.ValidationMessageFor(model => model.Animal.BVDHealthScheme)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.SalmonellaHealthScheme)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.SalmonellaHealthScheme, Model.SalmonellaType)
                    @Html.ValidationMessageFor(model => model.Animal.SalmonellaHealthScheme)
                </div>
    
                <div class="editor-label">
                    @Html.LabelFor(model => model.Animal.JohnesDiseaseHealthScheme)
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.Animal.JohnesDiseaseHealthScheme, Model.JohnesDiseaseHealthSchemes)
                    @Html.ValidationMessageFor(model => model.Animal.JohnesDiseaseHealthScheme)
                </div>
    
            </fieldset>
    
        </fieldset>
        <p><input type="submit" value="Save" /></p>
    
    }
    
    <div>
        @Html.ActionLink("Back to list", "Index")
    </div>
    

    Could not find a Surface controller route in the RouteTable for controller name AnimalController

    This means i need to use a controller for display and a controller for posting.....

    Has nobody a working solution without installing a complete framework?

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Aug 31, 2015 @ 19:13
    Sebastiaan Janssen
    0

    I think you're confusing 2 things: RenderMvcController and SurfaceController.

    You need a separate controller (inheriting from SurfaceController) to post your forms to. In that controller you process the posted model and do return RedirectToCurrentUmbracoPage(); when everything works out okay.

    Then the AnimalIndexmethod on your RenderMvcController will be used again.

  • Willem Luijk 24 posts 95 karma points
    Aug 31, 2015 @ 19:31
    Willem Luijk
    0

    Hi Sebastiaan, that is an scenario i read about and will ook like this. A new controller that inherits from SurfaceController:

    public class AnimalPostSurfaceController : SurfaceController
    {
    
        AnimalRepo animalRepo = new AnimalRepo();
    
        //
        // GET: /AnimalPost/
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AnimalEdit(AnimalViewModel animalViewModel)
        {
    
            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };
    
            if (animalViewModel.AnimalImageUpload != null && animalViewModel.AnimalImageUpload.ContentLength != 0)
            {
                if (!validImageTypes.Contains(animalViewModel.AnimalImageUpload.ContentType))
                {
                    ModelState.AddModelError("AnimalImageImageUpload", "Please choose either a GIF, JPG or PNG image.");
                }
            }
    
            if (ModelState.IsValid)
            {
    
                if (animalViewModel.AnimalImageUpload != null && animalViewModel.AnimalImageUpload.ContentLength > 0)
                {
                    var uploadDir = "~/App_Data/Uploads";
                    var fileName = string.Format("{0}{1}", animalViewModel.Animal.AnimalID, System.IO.Path.GetExtension(animalViewModel.AnimalImageUpload.FileName));
                    var imagePath = Path.Combine(Server.MapPath(uploadDir), fileName);
                    var imageUrl = Path.Combine(uploadDir, fileName);
    
                    if (animalViewModel.AnimalImageExist)
                        System.IO.File.Delete(animalViewModel.AnimalImagePathAbsolute);
    
                    animalViewModel.AnimalImageUpload.SaveAs(imagePath);
                    animalViewModel.Animal.ImageExt = System.IO.Path.GetExtension(animalViewModel.AnimalImageUpload.FileName);
                }
    
                animalRepo.Edit(animalViewModel.Animal);
                // return RedirectToAction("Index");
                return RedirectToCurrentUmbracoPage();
            }
    
            return View(animalViewModel);
        }
    
    }
    

    and a change the view, the form statement:

    @using (Html.BeginUmbracoForm("AnimalEdit", "AnimalPostSurface"))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
    
        <fieldset>
    

    This results in the notorious error:

    No parameterless constructor defined for this object.

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Aug 31, 2015 @ 19:38
  • Willem Luijk 24 posts 95 karma points
    Aug 31, 2015 @ 19:46
    Willem Luijk
    0

    Bingo! These extra constructors did the trick. Thanks a lot!

        public AnimalViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) {}
        public AnimalViewModel(IPublishedContent content) : base(content) { }
    
  • Willem Luijk 24 posts 95 karma points
    Sep 01, 2015 @ 05:55
    Willem Luijk
    0

    One more thing....

    Returning from a valid post went ok while i am using

    return RedirectToCurrentUmbracoPage();
    

    but what about returning from invalid post and sending the viewmodel back. In normal MVC we do this:

    return View("~/Views/AnimalEdit.cshtml", animalViewModel);
    

    but in Umbraco the viewmodel get malformed. Type information is lost and the YSOD appears.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 01, 2015 @ 10:22
    Jeroen Breuer
    0

    You can also use the SurfaceController and combine it with the IRenderMvcController interface to do both: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Controllers/Base/SurfaceRenderMvcController.cs#L14

    Now you can have 1 controller for route hijacking and posting.

    Jeroen

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Sep 01, 2015 @ 10:25
    Sebastiaan Janssen
    0

    @Willem:

    return View("~/Views/AnimalEdit.cshtml", animalViewModel);

    You should've been taught in the master classes that it's best to use:

    return CurrentUmbracoPage();
    

    when there's validation errors. This will get you back to the page that you were on with the model populated the way it was before you posted the form and errors added to it.

Please Sign in or register to post replies

Write your reply to:

Draft