1

Want to invoke AddPhoneNumber method in Manage controller from Account Controller Login Page. I have a checkbox in Login page which I have implemented for OTP.

What I want is that when checkbox is checked, it should invoke AddPhoneNumber GET method from Manage Controller to open the page.

Manage controller

 // GET: /Manage/AddPhoneNumber
        public ActionResult AddPhoneNumber()
        {
            return View();
        }

AddPhoneNumber page

@model Aayumitra.Models.AddPhoneNumberViewModel

        @using (Html.BeginForm("AddPhoneNumber", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <h4>Add a phone number</h4>
            <hr />
            @Html.ValidationSummary("", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(m => m.Number, new { @class = "col-md-5 control-label" })
                <div class="col-md-7">
                    @Html.TextBoxFor(m => m.Number, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" class="btn btn-primary" value="Send verification code" />
                </div>
            </div>
        }

Login Page

@using Aayumitra.Models
@model LoginViewModel

 @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()

                <div class="form-group">
                    <label for="email">Mobile Number / Email ID</label>

                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control form-control-sm", placeholder = "Mobile Number / Email ID" })
                    @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                </div>
                <div class="form-group">
                    <label for="password">Password</label>
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control form-control-sm password-input", placeholder = "Password" })
                    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                </div>
                <div class="form-group">
                     <input type="checkbox" name="remember-password" id="remember-pass-check" onclick="triggerLink()">
                    <label for="">Login with OTP instead of password</label>
                </div>
    }

function triggerLink() {
        debugger;
        var theUrl ='@Url.Action("AddPhoneNumber","Manage", Model)';
    }

1 Answers1

0

See: How do I redirect to another webpage?

In your triggerLink() method, something like:

<script type="text/javascript">
  function triggerLink() {
      debugger;
      var theUrl ='@Url.Action("AddPhoneNumber","Manage")';
      window.location.href = theUrl;
  }
</script>

Also, don' tthink you need to pass the model along, since your controller action does not look like it's expecting it...

Jonathan
  • 4,916
  • 2
  • 20
  • 37
  • It is not working. When It invokes, it uses this URL `http://localhost:49812/Account/Login?ReturnUrl=%2FManage%2FAddPhoneNumber`. And the page does not appear. –  May 29 '19 at 07:20
  • Hmmm. not sure what you've got different. I just recreated your code, and it worked with the following changes: wrapped your javascript in – Jonathan May 29 '19 at 19:14
  • Hi Jonathan, I have used `Individual User Accounts` to create my application where I can use default created login and register page. If we put checkbox in login page and write JQeury logic inside login page then it does not invoke to `AddPhoneNumber` which is under `Manage` controller. But strangely, If I create `No Authentication` application and do the same then it works. Please guide me why the behavior is different. –  May 29 '19 at 20:35
  • When you view source on the login page, what is the value in javascript for `theUrl`? – Jonathan May 29 '19 at 20:38
  • It gives me `/Manage/AddPhoneNumber` .But It appears like `http://localhost:49812/Account/Login?ReturnUrl=%2FManage%2FAddPhoneNumber` in the url. –  May 29 '19 at 20:41
  • You should use Fiddler or similar to track it down. What's likely happening is the javascript is redirecting correctly to '/Manage/AddPhoneNumber', but then *that* page requires authentication to load, and so is redirecting to 'Login', with the ReturnUrl. You need to disable authentication required for the '/Manage/AddPhoneNumber' endpoint – Jonathan May 29 '19 at 20:42
  • I will try to find a way to disable authentication required for this page –  May 29 '19 at 20:48