locked
Please help me , How to submit form with parameters without refreshing view cshtml this is my view cshtml and my action controller RRS feed

  • Question

  • User-22024077 posted

    View form : 

    <form id="myform" style=" margin-left: 300px; padding-bottom: 10px; padding-top: 30px;" method="post" action="/Home/Index">
                           <label style="margin-left: 50px;margin-right: -50px;" asp-for="Mois" for="exampleFormControlTextarea1"> Date</label>
                           <input style="margin-right: 100px;margin-left: 50px;" type="text" id="txtSelectedDate" name="date" readonly="readonly"/>
                           <label asp-for="Fam" class="control-label">Unité</label>
                           @Html.DropDownList("unite", null, htmlAttributes: new { @class = "js-select2", @style = "min-width: 300px;", id = "unitedrop" })
                           <input style="margin-left: 80px;" type="submit" value="envoyer" class="btn btn-primary">
     </form>

    Controller : 

      [HttpPost]
            public IActionResult Index(System.DateTime ? date, string unite)
            {

                DashboardViewModel dashboard = new DashboardViewModel();
                var Id = int.Parse(HttpContext.Session.GetInt32("id").ToString());
                dashboard.admins_count = _context.Admins.Count();
                dashboard.roles_count = _context.Roles.Count();

                dashboard.resultpie = (_context.Set<Piefamillegam>().FromSqlRaw("dbo.JournalProdGamme @date = {0}, @unite = {1}", date, unite)).ToList();
                dashboard.drilldown = new List<List<Piedrilldown>>();
                dashboard.resultpie.ForEach<Piefamillegam>(e => {
                    List<Piedrilldown> item = (_context.Set<Piedrilldown>().FromSqlRaw("dbo.JournalQuantProdParGamme @date = {0}, @unite = {1} ,@gamme ={2}", date, unite, e.Gamme)).ToList();
                    dashboard.drilldown.Add(item);
                });

                ViewBag.unite = new SelectList(_context.Funite, "Unite", "Libelle");

                dashboard.prodrealis = _context.Set<ProdRealis>().FromSqlRaw("dbo.PrevisionRealisationBetween @date = {0}, @unite = {1}", date, unite).ToList();
                ViewData["unite"] = new SelectList(_context.Funite, "Unite", "Libelle");


    );


                dashboard.fiches_count = _context.Fichtecfr.Count();
                if (Id == 17)
                    dashboard.fiche_demand = _context.Fichtecfr.Where(s => !(bool)s.Valide).Count();
                else
                    dashboard.fiche_demand = _context.Fichtecfr.Where(s => !(bool)s.Valide).Where(s => s.Id == Id).Count();

                return View(dashboard);
            }

    Tuesday, March 9, 2021 8:01 AM

All replies

  • User-474980206 posted

    This can only be done JavaScript and Ajax. 

    note: Microsoft has a unmaintained jquery library that is popular   

    https://github.com/aspnet/jquery-ajax-unobtrusive

    Tuesday, March 9, 2021 3:32 PM
  • User1686398519 posted

    Hi AspCoreDevsDz, 

    You can use ajax to achieve your needs.

    Based on the code you provided, I wrote an example, you can refer to it.

    Model

        public class DashboardViewModel
        {
    
            public int roles_count { get; internal set; }
            public int admins_count { get; internal set; }
            public string resultpie { get; internal set; }
        }

    Controller

            public IActionResult Index()
            {
                List<Students> data = new List<Students> { new Students {  Id=1, Name="test1"}, new Students { Id = 2, Name = "test2" } };
                ViewBag.test = new SelectList(data,"Id","Name");
                return View();
            }
            [HttpPost]
            public IActionResult Index(string datevalue, string unite)
            {
                var t = new Random();
                var date = String.IsNullOrEmpty(datevalue)? DateTime.MinValue  : DateTime.Parse(datevalue);
                DashboardViewModel dashboard = new DashboardViewModel {   roles_count = t.Next(1, 10), admins_count= t.Next(1, 10), resultpie=unite};
                return Json(dashboard);
            }

    View

    <div id="testcontent" style=" margin-left: 300px; padding-bottom: 10px; padding-top: 30px;">
        <input style="margin-right: 100px;margin-left: 50px;" type="text" id="txtSelectedDate" name="date" readonly="readonly" value="3/10/2021 12:45:34"/>
        @Html.DropDownList("unite", ViewBag.test as SelectList, htmlAttributes: new { @class = "js-select2", @style = "min-width: 300px;", id = "unitedrop" })
        <input id="submitbtn" style="margin-left: 80px;" type="button" value="envoyer" class="btn btn-primary">
    </div>
    <div id="showdata"></div>
    @section scripts{ 
        <script>
            $("#testcontent").on("click", "#submitbtn", function () {
                console.log("123")
                $.ajax({
                    url: "@Url.Action("Index", "TestAjax1")",
                    type:"Post",
                    data: { datevalue: $("#txtSelectedDate").val(), unite: $("#unitedrop").val() },
                    success: function (data) {
                        var content = '<ul class="list-group">'
                            + '<li class="list-group-item">' + data.admins_count + '</li>'
                            + '<li class="list-group-item">' + data.roles_count + '</li>'
                            + '<li class="list-group-item">' + data.resultpie + '</li></ul>';
                        $("#showdata").html(content);
                    },
                    error: function (response) {
                        console.log(response)
                    }
                });
            });
        </script>
    }

    Here is the result. 

    Best Regards,

    YihuiSun

    Wednesday, March 10, 2021 6:16 AM