Copied to clipboard

Flag this post as spam?

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


  • Scott 95 posts 277 karma points
    Nov 24, 2013 @ 01:58
    Scott
    0

    Creating form based on model in Partial Macro View

    Hi all,

    I am trying to figure out how to create a form based on a custom model in a Partial Macro View but I cannot seem to get it to work.

    It is just throwing an error that it cannot render the macro.

    Something like this: Error loading Partial View script

    I have seen a few having this kind of issue, however no solution.

    Are there anyone who have solved this yet?

    Regards,

    Scott

  • Scott 95 posts 277 karma points
    Nov 24, 2013 @ 19:55
    Scott
    100

    I actually found a solution to this

    Create a partial view and call that from the partial macro view using the model and writing out the form

  • Charles Afford 1163 posts 1709 karma points
    Nov 24, 2013 @ 21:01
    Charles Afford
    0

    Hi,

    What you need to do is call the partial view in the view.  In the partial view at the top do

    @model your model name here

    // rest of partial view

    make call to your surface controller using either

    @Html.BeginForm('actionname', controllername)

    @Ajax.BeginForm('actionname', controllername)

    :)  Hope this helps

     

     

     

     

  • Scott 95 posts 277 karma points
    Nov 25, 2013 @ 13:23
    Scott
    3

    Hi Charles,

    Thank you for responding, however I did it like this if anyone needs it later on:

    (only partly what I have but for the purpose it should give meaning)

    MembersModel.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Security;
    using System.Linq;
    using System.Web;
    
    namespace Models {
        public class ChangePasswordModel
        {
            [Required]
            [DataType(DataType.Password)]
            [Display(Name = "Nuværende kode")]
            public string OldPassword { get; set; }
    
            [Required]
            [StringLength(100, ErrorMessage = "{0} skal være minimum {2} karakterer langt.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "Ny kode")]
            public string NewPassword { get; set; }
    
            [DataType(DataType.Password)]
            [Display(Name = "Bekræft ny kode")]
            [Compare("NewPassword", ErrorMessage = "Ny kode og Bekræft ny kode er ikke ens.")]
            public string ConfirmPassword { get; set; }
        }
    }
    

    MembersController.cs

    using Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Profile;
    using System.Web.Security;
    using umbraco;
    using umbraco.cms.businesslogic.member;
    using Umbraco.Web.Mvc;
    
    namespace Controllers
    {
        public class MembersController : SurfaceController
        {
            [Authorize]
            public ActionResult ChangePassword()
            {
                return CurrentUmbracoPage();
            }
    
            [Authorize]
            [HttpPost]
            public ActionResult ChangePassword(ChangePasswordModel model)
            {
                bool changePasswordSucceeded = false;
                TempData["Success"] = changePasswordSucceeded;
    
                if (ModelState.IsValid)
                {
                    MembershipUser u = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                    try
                    {
                        changePasswordSucceeded = u.ChangePassword(model.OldPassword, model.NewPassword);
                    }
                    catch { }
    
                    if (changePasswordSucceeded)
                    {
                        TempData["Success"] = true;
                        return RedirectToCurrentUmbracoPage();
                    }
                    else
                    {
                        ModelState.AddModelError("", library.GetDictionaryItem("ChangePasswordInvalidPassword"));
                        return CurrentUmbracoPage();
                    }
                }
    
                // If we got this far, something failed, redisplay form
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    

    Partial View Macro Files ChangePassword.cshtml (I have allowed the macro to be included in a richtext editor, you could also include it in a view, keep reading)

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @Html.Partial("~/Views/Partials/ChangePassword.cshtml", new Models.ChangePasswordModel {})
    

    Partial View ChangePassword.cshtml

    @model Models.ChangePasswordModel
    @using Controllers
    @using Umbraco.Web
    @using Umbraco.Web.Macros
    @using Umbraco.Web.Mvc
    
    @if (TempData["Success"] != null)
    {
        if((bool)TempData["Success"])
        {
            @:Koden er ændret
        }
    }
    
    @using (Html.BeginUmbracoForm<MembersController>("ChangePassword"))
    {
        @Html.ValidationSummary(true, "Der opstod en fejl, ret venligst fejlen og prøv igen.")
        @Html.AntiForgeryToken()
        @Html.EditorFor(model => Model)
            <input type="submit" name="submit" />
    }
    

    View (Template) Page.cshtml

    <!DOCTYPE html>
    <head>
    </head>
    <body>
        @*In the content field which in my case is a RTE you just add the macro*@
        @Umbraco.Field("content")
    
        @*However if you want to do this manually just do like this*@
        @Html.Partial("ChangePassword", new Models.ChangePasswordModel { })
    
        <script src="@Url.Content("~/Scripts/d/jquery-1.10.2.min.js")"></script>
        <script src="@Url.Content("~/Scripts/d/jquery.validate.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/d/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    </body>
    </html>
    

    Hopefully this helps someone in need!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies