0

my problem is to, when open with my index.cshtml(Asp.net MVC) in visual studio(2017), run the project that time JavaScript and jQuery function working properly but after close that index.cshtml page again runs the same thing, that time JavaScript and jQuery function not working. I find out those things put the breakpoints in my controller. The purpose of the javascript, jQuery and ajax function is to if I choose the item name from the dropdown list that time price(inside the text box) should come automatically according to the item name.
here I show my code Index.cshtml

@model dynamic
@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
</head>
<body>
    <div class="container-scroller">
               <div class="main-panel">
            <div class="content-wrapper" style="background-color:#bbb9b9">

                <script>
                    $(document).ready(function () {
                        $("#itemname").change(function () {
                            var id = $("#itemname").val();
                            //alert(id);
                            var alldata = "id=" + id;
                            $.ajax({
                                type: "POST",
                                url: "GetPrice",
                                async: false,
                                data: alldata,

                                success: function ($data) {

                                    document.getElementById("p").value = $data;

                                }
                            });
                        });
                    });
                </script>

        <script>
                        function find_Item_id() {
                            var item_n = $("#itemname").val();

                            $.ajax({
                                type: "post",
                                url: "Find_ID_Of_Item",
                                data: { item_n: item_n },
                                success: function ($k) {
                                    //alert($k);
                                    document.getElementById("item_idd").value = $k;
                                }
                            });


                        }
                    </script>


                <h2>Customer Order</h2>
                               <form method="post" action="Final_Customer_Order">
                    <div class="row">                      
                        <div class="col-sm-3">
                            <p>Item Name</p>
                            <select name="item" id="itemname" onchange="find_Item_id()" class="form-control">
                                @foreach (var it in Model.item_data)
                                {
                                    <option value="@it.item_name">@it.item_name</option>
                                }
                            </select>
                            <input type="text" style="display:none" id="item_idd" value="" />
                        </div>


                        <div class="col-sm-3">
                            <p>Price </p><input type="text" name="price" id="p" class="form-control" />
                        </div>


                    </div>

                    <br />

                </form>
            </div>

                 </div>
        <!-- main-panel ends -->
    </div>


    <script src="~/vendors/js/vendor.bundle.base.js"></script>
    <script src="~/vendors/js/vendor.bundle.addons.js"></script>
    <script src="~/js/off-canvas.js"></script>
    <script src="~/js/misc.js"></script>
    <script src="~/js/dashboard.js"></script>
    </body>
</html>  

BillController.cs

 [HttpPost]
        public ActionResult Find_ID_Of_Item(string item_n)
        {
            var d = from k in db.Items.Where(x => x.item_name == item_n) select k;
            return View(d);
        }


        [HttpPost]
        public ActionResult GetPrice(string id)
        {
            List<Item> it = db.Items.Where(x => x.item_name == id).ToList();
            return View(it);
        }

Item.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Invoice.Models
{
    [Table("Item")]
    public class Item
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int item_id { get; set; }
        public string item_name { get; set; }
        public float price { get; set; }
    }
}

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/Content/jq")
    @Scripts.Render("~/bundles/scripts")
</head>
<body>

    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

BundleConfig.cs

public class BundleConfig
    {

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));


            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));
            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                     "~/Scripts/bootstrap.js",
                     "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
}

if anyone gives a proper solution, it will more helpful for me.

2 Answers 2

1

I think your index.cshtml not loading completely.can you check the url while script working time and not working time? Also, compare those things with Routeconfig.cs as well.

Sign up to request clarification or add additional context in comments.

Comments

0

remove your all script files and re-install the script package using Nu-Get package.sometime it will work for you.

1 Comment

yeah, I tried it but not work out. again the same thing happened

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.