locked
Syntax Error: Javascript RRS feed

  • Question

  • User1324715958 posted

    Not sure whats wrong. I'm little newb to mvc and javascript:

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
            <p id="login-paragraph">Email</p>
            <div><input type="text" class="login-text-input" id="Email" value=""></div>
            <p id="login-paragraph">Password</p>
            <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div"><a href="@Url.Action("Edit","Controller")" id="btnLogIn"><img src="@Url.Content("~/Content/Images/Log_In.png")" /></a></div>   
    </div>
    
    @section Scripts {
    <script type="text/javascript">
        $("#btnLogIn").click(function ()
        {
            var Email = $("#Email").val();
            var Password = $("#Password").val();
            window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
        });
    </script>

    Server Error in '/' Application.


    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

    Parser Error Message: Expected "}".


    Source Error: 

    Line 24:         window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
    Line 25:     });
    Line 26: </script>
    


    Source File: /Views/Home/Log_In.cshtml    Line: 26 


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3190.0

    Thursday, May 23, 2019 4:29 PM

Answers

  • User475983607 posted

    Not sure whats wrong. I'm little newb to mvc and javascript:

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
            <p id="login-paragraph">Email</p>
            <div><input type="text" class="login-text-input" id="Email" value=""></div>
            <p id="login-paragraph">Password</p>
            <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div"><a href="@Url.Action("Edit","Controller")" id="btnLogIn"><img src="@Url.Content("~/Content/Images/Log_In.png")" /></a></div>   
    </div>
    
    @section Scripts {
    <script type="text/javascript">
        $("#btnLogIn").click(function ()
        {
            var Email = $("#Email").val();
            var Password = $("#Password").val();
            window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
        });
    </script>

    Server Error in '/' Application.


    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

    Parser Error Message: Expected "}".


    Source Error: 

    Line 24:         window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
    Line 25:     });
    Line 26: </script>
    


    Source File: /Views/Home/Log_In.cshtml    Line: 26 


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3190.0

    There are several syntax and logical errors which are fixed below.

    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div>
            <input type="text" class="login-text-input" id="Email" value="test@mail.com">
        </div>
        <p id="login-paragraph">Password</p>
        <p>
            <input type="text" class="login-text-input" id="Password">
        </p>
        <div id="login-button-div">
            <a href="@Url.Action("Index","Ajax")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    
    @section scripts {
        <script>
            $("#btnLogIn").click(function (e)
            {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Login","Ajax")?email=' + Email;
            });
        </script>
    }
        public class AjaxController : Controller
        {
            // GET: Ajax
            [HttpGet]
            public ActionResult Index()
            {           
                return View();
            }
    
            public ActionResult Login(string email)
            {
                return Content(email);
            }

    Login should be a POST not A GET.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 5:31 PM
  • User1324715958 posted

    I fixed my error but I feel like this should be in the layout and not the view?

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div><input type="text" class="login-text-input" id="Email" value=""></div>
        <p id="login-paragraph">Password</p>
        <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div">
            <a href="@Url.Action("Log_In","Home")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    @section Scripts {
    <!--Put this in layout?-->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            $("#btnLogIn").click(function (e) {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Log_in","Home")?email=' + Email;
            });
        </script>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 7:20 PM

All replies

  • User753101303 posted

    Hi,

    Line 26 is your last line ? What if using :

    @section Scripts {
    <script type="text/javascript">
        $("#btnLogIn").click(function ()
        {
            var Email = $("#Email").val();
            var Password = $("#Password").val();
            window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
        });
    </script>
    } // To close your @section block

    Thursday, May 23, 2019 5:07 PM
  • User475983607 posted

    Not sure whats wrong. I'm little newb to mvc and javascript:

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
            <p id="login-paragraph">Email</p>
            <div><input type="text" class="login-text-input" id="Email" value=""></div>
            <p id="login-paragraph">Password</p>
            <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div"><a href="@Url.Action("Edit","Controller")" id="btnLogIn"><img src="@Url.Content("~/Content/Images/Log_In.png")" /></a></div>   
    </div>
    
    @section Scripts {
    <script type="text/javascript">
        $("#btnLogIn").click(function ()
        {
            var Email = $("#Email").val();
            var Password = $("#Password").val();
            window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
        });
    </script>

    Server Error in '/' Application.


    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

    Parser Error Message: Expected "}".


    Source Error: 

    Line 24:         window.location = "@Url.Action("TheAction","TheController")" + "/" + Email;
    Line 25:     });
    Line 26: </script>
    


    Source File: /Views/Home/Log_In.cshtml    Line: 26 


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3190.0

    There are several syntax and logical errors which are fixed below.

    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div>
            <input type="text" class="login-text-input" id="Email" value="test@mail.com">
        </div>
        <p id="login-paragraph">Password</p>
        <p>
            <input type="text" class="login-text-input" id="Password">
        </p>
        <div id="login-button-div">
            <a href="@Url.Action("Index","Ajax")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    
    @section scripts {
        <script>
            $("#btnLogIn").click(function (e)
            {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Login","Ajax")?email=' + Email;
            });
        </script>
    }
        public class AjaxController : Controller
        {
            // GET: Ajax
            [HttpGet]
            public ActionResult Index()
            {           
                return View();
            }
    
            public ActionResult Login(string email)
            {
                return Content(email);
            }

    Login should be a POST not A GET.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 5:31 PM
  • User1324715958 posted

    Fixed a error but still not calling the controller or sending data:

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div><input type="text" class="login-text-input" name="Email" value=""></div>
        <p id="login-paragraph">Password</p>
        <p><input type="text" class="login-text-input" name="Password"></p>
        <div id="login-button-div">
            <a href="@Url.Action("Log_In","Home")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    @section JavaScript {
        <script>
            $("#btnLogIn").click(function (e) {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Login","Home")?email=' + Email;
            });
        </script>
    }

    Controller code:

      [HttpPost]
            public ActionResult Log_In(string Email)
            {
    
                return View();
            }

    What im trying to do is pass the data from the view to the controller. Next, i will make a call to my API to validate login. 

    Thursday, May 23, 2019 6:15 PM
  • User475983607 posted

    For some reason you changed the @section from Scripts to JavaScript.

    @section JavaScript {

    It should be 

    @section Scripts {

    The layout page defines a Scripts section which places your scripts at the bottom of the HTML page.

    Thursday, May 23, 2019 6:28 PM
  • User1324715958 posted

    Sorry I'll change that one sec

    Thursday, May 23, 2019 7:05 PM
  • User1324715958 posted

    Not sure what this means: Uncaught ReferenceError: $ is not defined

    I also fixed a few other mistakes in my code.

    New Code:

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div><input type="text" class="login-text-input" id="Email" value=""></div>
        <p id="login-paragraph">Password</p>
        <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div">
            <a href="@Url.Action("Log_In","Home")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    @section Scripts {
        <script>
            $("#btnLogIn").click(function (e) {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Log_in","Home")?email=' + Email;
            });
        </script>
    }

    Thursday, May 23, 2019 7:16 PM
  • User1324715958 posted

    I fixed my error but I feel like this should be in the layout and not the view?

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div><input type="text" class="login-text-input" id="Email" value=""></div>
        <p id="login-paragraph">Password</p>
        <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div">
            <a href="@Url.Action("Log_In","Home")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    @section Scripts {
    <!--Put this in layout?-->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            $("#btnLogIn").click(function (e) {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Log_in","Home")?email=' + Email;
            });
        </script>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 7:20 PM
  • User475983607 posted

    I fixed my error but I feel like this should be in the layout and not the view?

    @{
        ViewBag.Title = "Log_In";
    }
    
    <h2>Log In</h2>
    <br />
    <br />
    <br />
    <div id="login-div">
        <p id="login-paragraph">Email</p>
        <div><input type="text" class="login-text-input" id="Email" value=""></div>
        <p id="login-paragraph">Password</p>
        <p><input type="text" class="login-text-input" id="Password"></p>
        <div id="login-button-div">
            <a href="@Url.Action("Log_In","Home")" id="btnLogIn">
                <img src="@Url.Content("~/Content/Images/Log_In.png")" />
            </a>
        </div>
    </div>
    
    @section Scripts {
    <!--Put this in layout?-->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            $("#btnLogIn").click(function (e) {
                e.preventDefault();
                var Email = $("#Email").val();
                var Password = $("#Password").val();
                window.location = '@Url.Action("Log_in","Home")?email=' + Email;
            });
        </script>
    }

    Honestly, a login should be an HTTP POST not a GET.  HTTP GET is cached by your browser.  

    It's up to you to design the login.  If you want the login in the layout and part of every page in your app then go ahead.

    Before moving forward though, I recommend going through the ASP.NET Identity tutorials on this site.  ASP.NET Identity is an API and data store for managing logins.

    https://docs.microsoft.com/en-us/aspnet/identity/overview/

    Thursday, May 23, 2019 7:34 PM