Copied to clipboard

Flag this post as spam?

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


  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 11, 2015 @ 08:22
    Sören Deger
    0

    Model in Surface is Controller is null

    Hi all,

    I have a very strange issue with a simple surface controller in 7.2.8.

    This is the partial view:

    @using themenseiten.businesslogic.Controller
    
    @model themenseiten.businesslogic.Models.myLoginModel
    
    @{
        using (Html.BeginUmbracoForm<myLoginController>("Authenticate"))
        {
            <div class="shortFields">
                <div class="form-group">
                    @Html.LabelFor(m => m.Login)
                    @Html.TextBoxFor(m => m.Login, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Login)
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password)
                    @Html.TextBoxFor(m => m.Password, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Password)
                </div>
            </div>
            <input type="submit" value="Login" class="btn"  /> <a href="/">Passwort vergessen?</a> 
        }
    }
    

    This is the Surface Controller:

    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using themenseiten.businesslogic.API;
    
    
    namespace themenseiten.businesslogic.Controller
    {
        public class myLoginController : SurfaceController
        {
            public myLoginController() { }
    
            [HttpPost]
            public ActionResult Authenticate(themenseiten.businesslogic.Models.myLoginModel login)
            {
                var memberApi = new chbeckMembership();
    
                return Content(memberApi.isAuthenticated(login.Login, login.Password).ToString());
            }
        }
    }
    

    And this is the model:

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel;
    
    namespace themenseiten.businesslogic.Models
    {
        [Serializable]
        public class myLoginModel
        {
            [Required]
            [DisplayName("Benutzername")]
            public string Login { get; set; }
            [DisplayName("Passwort")]
            [Required]
            public string Password { get; set; }
        }
    }
    

    If I click on the "Login"-Button, the "Authenticate"-Method is called, but the myLoginModel contains the value "null".

    Have anyone an idea and can help me?

    Cheers,

    Sören

  • Charles Afford 1163 posts 1709 karma points
    Sep 11, 2015 @ 09:50
    Charles Afford
    0

    Hi, I would try

    taking the [Serializable] of the model Posting using HTML.begin form

    That way at least you can zero in on the problem.

    Nothing I can see looks really wrong they are only string properties.

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 11, 2015 @ 10:33
    Sören Deger
    0

    Hi Charles,

    thanks, but this don't solved the problem. The model is each time null.

  • Charles Afford 1163 posts 1709 karma points
    Sep 11, 2015 @ 10:40
    Charles Afford
    0

    Ok so try removing the second parameter from textboxfor? I wonder if the parameters are wrong on the input

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 11, 2015 @ 10:49
    Sören Deger
    0

    Same result :( model is null

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 11, 2015 @ 12:32
    Sören Deger
    0

    Very very strange... I have two projects in visual studio. One Project "themenseiten.businesslogic" with the model and controller and one project "themenseiten.web" with the umbraco site and the views.

    Now, I have copy the same model and controller in the themenseiten.web project and all works great. But in the first project the model is null.

    It seems to be a configuration problem with references and other things in visual studio.

    Has someone an idea?

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 11, 2015 @ 12:54
    Sören Deger
    100

    I have solved it!!!

    But the cause for this issue is incomprehensible and impossible for me!

    The issue occurs if I call my model in action method of surface controler "login":

    public ActionResult Authenticate(myLoginModel login)
    

    If I rename the variable, i.e. "tModel", then it works great:

    public ActionResult Authenticate(myLoginModel tModel)
    

    It seems, that there exist an global model with the name "login" and this cause the conflicts.

    Is there somewhere an overview of such global variables?

    Cheers,

    Sören

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Sep 12, 2015 @ 15:20
    Shannon Deminick
    1

    There are no secret variable names and the model binding happens with normal mvc, I suspect it's something to do with you passing in a value on your button called 'login' and you have a property in your model called login, probably confusing the model binder. Much easier to debug such things if you see what is actually posted in chrome dev tools or fiddler

Please Sign in or register to post replies

Write your reply to:

Draft