Copied to clipboard

Flag this post as spam?

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


  • Wilson 14 posts 103 karma points
    Aug 03, 2017 @ 03:26
    Wilson
    0

    how to add c# code behind for a partial view

    Hi, i have below partial views for user login. I need to execute some c# code when the login button click, can someone guide on how to do it.

    Thanks.

        <fieldset class="form">
    
            @Html.LabelFor(m => login.Username)
            @Html.TextBoxFor(m => login.Username)
            <br />
    
            @Html.LabelFor(m => login.Password)
            @Html.PasswordFor(m => login.Password)
            <br />
    
            <button class="button">Login</button>
        </fieldset>  
    
  • Manish 373 posts 932 karma points
    Aug 03, 2017 @ 06:29
    Manish
    0

    You can write Ajax call on click event.

    Thanks

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 03, 2017 @ 06:38
    David Brendel
    2

    Hi Wilson,

    as Manish mentioned you could send the form via ajax to a SurfaceController and then process the form.

    Or you have to make a post request to a SurfaceController. Then you have to use BeginUmbracoForm to set up the routing where the data should be posted to.

    Regards David

  • Wilson 14 posts 103 karma points
    Aug 03, 2017 @ 07:04
    Wilson
    0

    Hi, the requirements is c# is needed for the solution not ajax.

  • Manish 373 posts 932 karma points
    Aug 03, 2017 @ 07:05
    Manish
    0

    You can call a controller which have code of c# using Ajax. Hope you got my point.

  • John ben 78 posts 293 karma points
    Aug 03, 2017 @ 09:04
    John ben
    1

    Hi.

    you can create a controller who inherit from SurfaceController

    in ur view u will have something like this :

          using (Html.BeginUmbracoForm("MemberLogin", "Membership", FormMethod.Post, new { @class = "form-signin" }))
                {
                    @Html.AntiForgeryToken()
                    <h1 class="page-title fadeInUp animated">@ViewBag.Name</h1>
                    <div class="row">
                        <div class="form-group col-md-6 col-sm-12">
                            @Html.LabelFor(m => m.Login, new { @class = "room" })
                            @Html.TextBoxFor(m => m.Login, new { @class = "form-control", placeholder = Html.DisplayNameFor(n => n.Login) })
                            @Html.ValidationMessageFor(model => model.Login, "", new { @class = "text-danger" })
                        </div>
                        <div class="form-group col-md-6 col-sm-12">
                            @Html.LabelFor(m => m.Password, new { @class = "room" })
                            @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = Html.DisplayNameFor(n => n.Password) })
                            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row lastchildrow">
                        <input type="submit" class="btn btntaille" value="@Resources.Content.btn_txt_Envoyer">
                    </div>
                }
    

    and in ur controller something like this :

      public class MembershipController : SurfaceController
        {
            private const string VIEW_FOLDER_PATH = "~/Views/Partials/Membership/";
    
            public List<AvocatVM> GetMembers()
            {
                var ListAvocat = DbHelper.CurrentDbTest().Fetch<AvocatVM>(new Sql().Select("*").From("AVOCAT"));
                return ListAvocat;
            }
    
            [HttpGet]
            [ActionName("MemberLogin")]
            public ActionResult Index()
            {
                IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "authentification").FirstOrDefault();
                Urls Filiale = new Urls(CurrentPage.Name, CurrentPage.Url);
                ViewBag.File = Filiale;
                ViewBag.Name = CurrentPage.Name;
                var root = Thread.CurrentThread.CurrentCulture.Name.Substring(0, 2);
                TempData["root"] = root;
                var Url = CurrentPage.Url;
                TempData["url"] = Url;
                return PartialView(VIEW_FOLDER_PATH + "_LoginForm.cshtml", new LoginViewModel());
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            [ActionName("MemberLogin")]
            public ActionResult Validate(LoginViewModel model)
            {
                var listmember = GetMembers();
                foreach (var item in listmember)
                {
                    if(item.Email== model.Login && item.Password == model.Password)
                    {
                        var response = Request["g-recaptcha-response"];
                        string secretKey = "6LfJeQgUAAAAAEhworh47HsU1-zSUH3LlWmLd3yx";
                        var webClient = new WebClient();
                        var result = webClient.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, response));
                        var obj = JObject.Parse(result);
                        var status = (bool)obj.SelectToken("success");
                        //if (ModelState.IsValid && status == true)
                        //{
                            FormsAuthentication.SetAuthCookie(model.Login, model.RememberMe);
                            return RedirectToCurrentUmbracoPage();
                        //}
                    }
                }
                TempData["Status"] = Resources.Content.Invalidlogs.ToString();
                return RedirectToCurrentUmbracoPage();
            }
    
            [HttpGet]
            public ActionResult Logout()
            {
                Session.Clear();
                FormsAuthentication.SignOut();
                return Redirect("~/" + TempData["Url"]);
    
            }
    
    
        }
    

    in my GetMembers i m getting data from another database so you can just change it from where you want

    Hope it helped you.

    Best Regards.

  • Wilson 14 posts 103 karma points
    Aug 06, 2017 @ 08:22
    Wilson
    0

    can you share with me how you show

    TempData["Status"]

    in your template ?

  • John ben 78 posts 293 karma points
    Aug 07, 2017 @ 08:31
    John ben
    0

    the entire view :

      @if (User.Identity.IsAuthenticated)
                {
                    <p>
                        @Resources.Content.Bienvenue, @User.Identity.Name
                    </p>
                    <p>
                        @Html.ActionLink(Resources.Content.Logout, "Logout", "Membership")
                    </p>
    
                }
                else
                {
                    using (Html.BeginUmbracoForm("MemberLogin", "Membership", FormMethod.Post, new { @class = "form-signin" }))
                    {
                        @Html.AntiForgeryToken()
                        <h1 class="page-title fadeInUp animated">@ViewBag.Name</h1>
                        <div class="row">
                            <div class="form-group col-md-6 col-sm-12">
                                @Html.LabelFor(m => m.Login, new { @class = "room" })
                                @Html.TextBoxFor(m => m.Login, new { @class = "form-control", placeholder = Html.DisplayNameFor(n => n.Login) })
                                @Html.ValidationMessageFor(model => model.Login, "", new { @class = "text-danger" })
                            </div>
                            <div class="form-group col-md-6 col-sm-12">
                                @Html.LabelFor(m => m.Password, new { @class = "room" })
                                @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = Html.DisplayNameFor(n => n.Password) })
                                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="row lastchildrow">
                            <input type="submit" class="btn btntaille" value="@Resources.Content.btn_txt_Envoyer">
                        </div>
                    }
                    <p>@TempData["Status"]</p>
                }
    
  • Wilson 14 posts 103 karma points
    Aug 04, 2017 @ 00:41
    Wilson
    0

    Hi thanks for reply. I just start learning umbraco few days ago. just to check, if i need to create controller, is that necessary need to use visual studio or other editor ? Currently i am using Umbraco cloud and back office for the project. If i can create controller through back office, any document i can refer or any guide how to create controller through back office ?

  • John ben 78 posts 293 karma points
    Aug 04, 2017 @ 08:17
    John ben
    0

    Hi Wilson,

    I don't think that you can create controller with umbraco cloud , i advice you to use Visual studio, the persons who use just umbraco cloud are obliged to write there code in the view which i don't think it's very appropriate, anyway if you are still new with umbraco i don't thing there is better guide than https://www.youtube.com/watch?v=VWy2HlRDCRM&list=PL90L_HquhD--OWQNLyO1-KRxVDd0NPBfZ ( Paul Seal ) those videos helped me a lot, hoping it will help you too.

    Best Regards.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Aug 04, 2017 @ 08:50
    Dave Woestenborghs
    1

    Hi John

    It's not true that you need to write all your code in the view when using Umbraco Cloud.

    You can use VS without any problems. : https://our.umbraco.org/documentation/Umbraco-Cloud/Set-Up/Visual-Studio/

    Dave

  • Wilson 14 posts 103 karma points
    Aug 04, 2017 @ 08:52
    Wilson
    0

    Thanks. your comment really help me a lot. At least i know now i have to use Visual Studio instead of keep searching on the net for umbraco cloud how to create controller.

    thanks a lot.

  • Rune Hem Strand 147 posts 911 karma points hq c-trib
    Aug 06, 2017 @ 15:49
    Rune Hem Strand
    1

    Hi Wilson

    You don't HAVE to use Visual Studio (although it is great for MVC). You can create controller in eg. /App_Code/Controllers/, models in /App_Code/Models/ and so forth just as you can with any other .Net application. Cloud is not different in that sense, it is "just" an installation of Umbraco ☺️

    There is an exampple of how to do this in the Getting Started docs: https://our.umbraco.org/documentation/Getting-Started/Code/Creating-Forms/

    Hope that helps!

    /Rune

  • John ben 78 posts 293 karma points
    Aug 07, 2017 @ 08:06
    John ben
    0

    Hi Rune Hem Strand.

    Would like to know how would you generate the dll without VS?

    Best regards

  • Rune Hem Strand 147 posts 911 karma points hq c-trib
    Aug 07, 2017 @ 11:47
    Rune Hem Strand
    0

    That is a wonderful question!

    My bad :/ Your class files should be in the /App_Code/ folder. They will be compiled at runtime.

    So instead of creating *.cs in /Models/ and /Controllers/ they should be in /App_Code/. Will also work with sub directories, so you can do /App_Code/Models/ and /App_Code/Controllers/ to make things a little more clean.

    Sorry for the confusion!

  • 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