Copied to clipboard

Flag this post as spam?

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


  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 12:34
    Priya
    0

    How to call partial view in macro and display in content ?

    Hi Guys,

    I am using Umbraco 7.1.6 version for my project.

    I have created one page called login.cshtml in Views -> Partials.

    I would like it to be available in the content rich text editor.

    I have tried to create macro and gave login.cshtml file name in Razor script. but it is not working.

    can anyone help me to create a macro with .cshtml file and call it in content page ?

    Thanks, Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 12:49
    Yasir Butt
    0

    Hi,

    It is simple one.

    in your macro file you just have one line of code

    @Html.Action("Index","LoginSurface")
    

    In your Surface controller

    public ActionResult Index()
        {
            return PartialView("LoginForm");
        }
    

    Hope this will help

    Yasir

  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 13:11
    Priya
    0

    Hi Yasir,

    Thank you very much for you reply.

    I am new with this umbraco development.

    I have created following code under settings -> partial views

    @model CompanyShop.Web.Models.AgentLoginModel @if (User.Identity.IsAuthenticated) {

    Logged in: @User.Identity.Name

    @Html.ActionLink("Log out", "AgentLogout", "AgentLogin")

    Register new customer

    <a href="/OutstandingForms">Process oustanding forms</a>    
    

    } else { using (Html.BeginUmbracoForm("AgentLogin", "AgentLogin"))
    {

    <fieldset>
        <legend>Login</legend>
    
        @Html.ValidationSummary("loginModel", true)
    
        @Html.LabelFor(m => Model.UserName)
        @Html.TextBoxFor(m => Model.UserName)
        @Html.ValidationMessageFor(m => Model.UserName)
        <br />
    
        @Html.LabelFor(m => Model.Password)
        @Html.PasswordFor(m => Model.Password)
        @Html.ValidationMessageFor(m => Model.Password)
        <br />
    
        @Html.LabelFor(m => Model.RememberMe)
        @Html.CheckBoxFor(m => Model.RememberMe)
        @Html.ValidationMessageFor(m => Model.RememberMe)
        <br />
    
        <button>Login</button>
    </fieldset>          
    
    }
    
    <p>@TempData["Status"]</p>
    

    }

    Now in macro file where to write @Html.Action("Index","LoginSurface") code ?

    In macro I have got following : Name : Alias :

    Choose a file to render :

    MVC partial view: xslt: user control: Razor contro:

    Editor settings : Use in rich text box render in rich text editor : catch settings can you please help me where to write that code ?

    Thanks, Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 13:26
    Yasir Butt
    0

    Hi Priya

    It should write in MVC partial view.

    Yasir

  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 13:52
    Priya
    0

    Hi Yasir,

    Thanks for the reply

    I have got following code in surface controller.

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Mime; using System.Web; using System.Web.Mvc; using System.Web.Security; using CompanyShop.Web.Models; using Umbraco.Core; using Umbraco.Web.Mvc;

    namespace CompanyShop.Web.Controllers { public class AgentLoginController : SurfaceController { [HttpGet] [ActionName("AgentLogin")] public ActionResult AgentLoginGet() { return PartialView("_AgentLogin", new AgentLoginModel()); }

        [HttpPost]
        [ActionName("AgentLogin")]
        public ActionResult AgentLoginPost(AgentLoginModel model)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                return RedirectToCurrentUmbracoPage();
            }
            TempData["Status"] = "Invalid username or password";
            return RedirectToCurrentUmbracoPage();
        }
    
        [HttpGet]
        public ActionResult AgentLogout()
        {
            Session.Clear();
            FormsAuthentication.SignOut();
            return Redirect("/");
        }
    
    }
    

    }

    Do I need to add anything else here ?

    Thanks, Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 14:04
    Yasir Butt
    0

    No i dont think so. 

    It looks good. Are you getting any error?

     

    Yasir

  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 14:11
    Priya
    0

    Hi Yasir,

    when I tried to call that macro in content rich text editor it giving me following error:

    Error loading partial view script(file:~views/macropartials/comlogin.cshtml)

    I am not able to understand what is the problem ?

    Priya

  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 14:14
    Priya
    0

    I wrote following in partial view macro file.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @Html.Action("Index","AgentLoginController")

    does this right ?

    Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 14:35
    Yasir Butt
    0

    I assumed that your partial is under sub folder some where.

    if it is the case then you should pass the full path in PartialView for example.

    return PartialView("~/Views/Partials/Login/_AgentLogin.cshtml", new AgentLoginModel());
    

    if it is not the case then you should add the break point on your AgentLoginGet action method and see what causing the error

    Yasir

  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 14:44
    Priya
    0

    Hi yasir,

    I did change the path. I think I wrote something wrong in macro partial file. can you please tell what exactly I have to write in macro partial ?

    I wrote like :

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @Html.Action("AgentLogin","AgentLoginModel")

    Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 15:00
    Yasir Butt
    0

    Hi,

    It should be

    @Html.Action("ActionMethod", "Controller")

    in your case

    @Html.Action("AgentLoginGet","AgentLogin")
    

    Yasir

  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 15:14
    Priya
    0

    Hi yasir,

    I have changed it but still getting same error.

    I have tried to add bread point also but I didn't find anything :(

    Can you check this code as well may be something wrong in this

    @model CompanyShop.Web.Models.AgentLoginModel @if (User.Identity.IsAuthenticated) {

    Logged in: @User.Identity.Name

    @Html.ActionLink("Log out", "AgentLogout", "AgentLogin")

    Register new customer

    <a href="/OutstandingForms">Process oustanding forms</a>    
    

    } else { using (Html.BeginUmbracoForm("AgentLogin", "AgentLogin"))
    {

    <fieldset>
        <legend>Login</legend>
    
        @Html.ValidationSummary("loginModel", true)
    
        @Html.LabelFor(m => Model.UserName)
        @Html.TextBoxFor(m => Model.UserName)
        @Html.ValidationMessageFor(m => Model.UserName)
        <br />
    
        @Html.LabelFor(m => Model.Password)
        @Html.PasswordFor(m => Model.Password)
        @Html.ValidationMessageFor(m => Model.Password)
        <br />
    
        @Html.LabelFor(m => Model.RememberMe)
        @Html.CheckBoxFor(m => Model.RememberMe)
        @Html.ValidationMessageFor(m => Model.RememberMe)
        <br />
    
        <button>Login</button>
    </fieldset>          
    
    }
    
    <p>@TempData["Status"]</p>
    

    }

    Thanks,

    Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 15:32
    Yasir Butt
    0

    Error is in your form code which your pasted

    you cant write the text directly inside the razor. you have to write the text in variable or other ways then print it

    i changed it like below but you can change it

    @string.Format("Logged in: {0}", @User.Identity.Name);
    
    @Html.ActionLink("Log out", "AgentLogout", "AgentLogin");
    
    @string.Format("Register new customer")
    <a href="/OutstandingForms">Process oustanding forms</a>
    
  • Priya 29 posts 89 karma points
    Nov 06, 2014 @ 15:54
    Priya
    0

    I did try to change this code but still same error.

    don't know what wrong now ?

    :(

    Priya

  • Yasir Butt 161 posts 371 karma points
    Nov 06, 2014 @ 16:22
    Yasir Butt
    0

    Try this one in your view. if any name is not matched with your controller or action please change it according to yours

    @{
    if (User.Identity.IsAuthenticated)
    {
    
        var loggedIn = "Logged in: " + User.Identity.Name;
        @loggedIn
    
        var register = "Register new customer";
        @register
        <a href="/OutstandingForms">Process oustanding forms</a>
    }
    
    else
    {
        using (Html.BeginUmbracoForm<AgentLoginController>("AgentLogin"))
        {
    
            <fieldset>
                <legend>Login</legend>
    
                @Html.ValidationSummary("loginModel", true)
    
                @Html.LabelFor(m => Model.UserName)
                @Html.TextBoxFor(m => Model.UserName)
                @Html.ValidationMessageFor(m => Model.UserName)
                <br />
    
                @Html.LabelFor(m => Model.Password)
                @Html.PasswordFor(m => Model.Password)
                @Html.ValidationMessageFor(m => Model.Password)
                <br />
    
                <button>Login</button>
            </fieldset>
    
        }
    
        <p>@TempData["Status"]</p>
    
    
    
    }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft