Copied to clipboard

Flag this post as spam?

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


  • Alexander Mounzer 8 posts 118 karma points
    Jul 05, 2021 @ 18:02
    Alexander Mounzer
    0

    Inherit Umbraco propertiy values to a custom model

    I am very new to Umbraco and I need help understanding how I can inherit umbraco modelproperties to my custom model loginViewModel.

    My loginViewModel is used on a view called Mypages (MinaSidor). And i like to enable some part of the view to be modified by the umbraco backoffice users. They should be able to set new bodyText and etc.

    I am trying to map my view to the umbracos PublishedModels.

    My @Model has the context of LoginviewModel and the ContenModels has the context of MinaSidor, (Umbraco.Web.PublishedModels.MinaSidor;)

    I have tried to implement IpublishedContent on my model but I keep generating NullreferenceException whenever i call any action from my controller.

    View;

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<legimusWeb.ViewModels.LoginViewModel>
    @using ContentModels = Umbraco.Web.PublishedModels.MinaSidor;
    @{
    Layout = "Master.cshtml";
    }
    <h1>@Model.UserInformationModel.userRole</h1>
    <h1>@ContentModels.GetModelPropertyType(x => x.BodyText)</h1>
    @if (User.Identity.IsAuthenticated)
    { [...]
    

    Model without IpublishContent

    public class LoginViewModel
    {
        private IPublishedContent _publishedContent;
        public LoginViewModel() { }
    
        [Required]
        [DisplayName("Epost")]
        [DataType(DataType.EmailAddress)]
        public string Username { get; set; }
        [Required]
        [DisplayName("Lösenord")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        public UserInformationModel UserInformationModel { get; set; }
    

    If the solution is to inherit from Ipublishcontent in my loginViewModel Could anyone give an example to avoid the nullreference exception on the controller.

    Controller:

    [HttpPost] [ValidateAntiForgeryToken] public async Task

            try
            {
                var response = await _authenticationService.LoginUserAndGetTicketAsync(new AuthorizeCurrentUserInputModel()
                {
                    ndsUserName = loginViewModel.Username,
                    ndsUserPasswd = loginViewModel.Password
                });
                success = response.Item1;
                ticket = response.Item2;
    
            } catch (Exception e)
            {
                this.ModelState.AddModelError(String.Empty, @"Något gick fel.");
                return View("Login", loginViewModel);
            }
    
            if (success)
            {
                FormsAuthentication.SetAuthCookie(loginViewModel.Username, true);
                Session["ticketkey"] = ticket;
                if (!Request.RawUrl.Contains("/mina-sidor/"))
                {
                    return Redirect("/");
                }
                //Mina sidor redirect
                return Redirect("/mina-sidor/");
            }
            else
            {
                this.ModelState.AddModelError(String.Empty, @"Användarnamn eller lösenord är fel.");
            }
    
            return View("Login", loginViewModel);
        }
    
  • 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