Asp.net MVC Post Data To Controller without refreshing page [duplicate]












0















This question already has an answer here:




  • Send post data from html FORM with Javascript?

    5 answers




I am posting a selected value from Dropdown List. but after getting my value page goes refresh. How to post data without page refresh to controller?



Here is view:



<div class="container">
<div class="row">
@foreach (var item in Model)
{
using (Html.BeginForm("AddToCart", "Test", new { id = item.ProductId }, FormMethod.Post))
{


<div class="col-md-3 col-sm-4 col-xs-6">
<img id="ImageClick" onclick="location.href='@Url.Action("ViewProductOnClick", "Home", new { id = item.ProductId })'"
src="@Url.Content("~/Content/" + item.ImageURL)" height="200" width="200" alt="@item.ProductName" />
<div class="productDetails">
<div class="productName">
<h5 id="ProductName" class="bold name">Name: @item.ProductName</h5>
</div>

<div class="productPrice bold" id="ProductPrice">
Rs. <span class="unit"> @item.Price</span>
</div>
<div class="productCart">


<div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 no-padding">
<input type="submit" class="btn btn-success" id="btnSubmit" value="Add to Cart" />
</div>

</div>
</div>

</div>
}
}

</div>
</div>


Here is my controller:



    [HttpPost]
public ActionResult AddToCart(int id, FormCollection collection)
{
MyDBContext myDBContext = new MyDBContext();

if (ModelState.IsValid)
{
var cartFill = myDBContext.Products.Find(id);

int quantity = Convert.ToInt16(collection["Weight"]);

AddToCart addToCart = new AddToCart(cartFill, quantity);

if (Session["cart"] == null)
{
List<AddToCart> list = new List<AddToCart>();
list.Add(addToCart);
Session["cart"] = list;
}
else
{
List<AddToCart> list = (List<AddToCart>)Session["cart"];
list.Add(addToCart);
Session["cart"] = list;
}
}
return RedirectToAction("ViewCart", "Home");
}









share|improve this question















marked as duplicate by user3559349 Nov 16 '18 at 0:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Send post data from html FORM with Javascript?

      5 answers




    I am posting a selected value from Dropdown List. but after getting my value page goes refresh. How to post data without page refresh to controller?



    Here is view:



    <div class="container">
    <div class="row">
    @foreach (var item in Model)
    {
    using (Html.BeginForm("AddToCart", "Test", new { id = item.ProductId }, FormMethod.Post))
    {


    <div class="col-md-3 col-sm-4 col-xs-6">
    <img id="ImageClick" onclick="location.href='@Url.Action("ViewProductOnClick", "Home", new { id = item.ProductId })'"
    src="@Url.Content("~/Content/" + item.ImageURL)" height="200" width="200" alt="@item.ProductName" />
    <div class="productDetails">
    <div class="productName">
    <h5 id="ProductName" class="bold name">Name: @item.ProductName</h5>
    </div>

    <div class="productPrice bold" id="ProductPrice">
    Rs. <span class="unit"> @item.Price</span>
    </div>
    <div class="productCart">


    <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 no-padding">
    <input type="submit" class="btn btn-success" id="btnSubmit" value="Add to Cart" />
    </div>

    </div>
    </div>

    </div>
    }
    }

    </div>
    </div>


    Here is my controller:



        [HttpPost]
    public ActionResult AddToCart(int id, FormCollection collection)
    {
    MyDBContext myDBContext = new MyDBContext();

    if (ModelState.IsValid)
    {
    var cartFill = myDBContext.Products.Find(id);

    int quantity = Convert.ToInt16(collection["Weight"]);

    AddToCart addToCart = new AddToCart(cartFill, quantity);

    if (Session["cart"] == null)
    {
    List<AddToCart> list = new List<AddToCart>();
    list.Add(addToCart);
    Session["cart"] = list;
    }
    else
    {
    List<AddToCart> list = (List<AddToCart>)Session["cart"];
    list.Add(addToCart);
    Session["cart"] = list;
    }
    }
    return RedirectToAction("ViewCart", "Home");
    }









    share|improve this question















    marked as duplicate by user3559349 Nov 16 '18 at 0:34


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • Send post data from html FORM with Javascript?

        5 answers




      I am posting a selected value from Dropdown List. but after getting my value page goes refresh. How to post data without page refresh to controller?



      Here is view:



      <div class="container">
      <div class="row">
      @foreach (var item in Model)
      {
      using (Html.BeginForm("AddToCart", "Test", new { id = item.ProductId }, FormMethod.Post))
      {


      <div class="col-md-3 col-sm-4 col-xs-6">
      <img id="ImageClick" onclick="location.href='@Url.Action("ViewProductOnClick", "Home", new { id = item.ProductId })'"
      src="@Url.Content("~/Content/" + item.ImageURL)" height="200" width="200" alt="@item.ProductName" />
      <div class="productDetails">
      <div class="productName">
      <h5 id="ProductName" class="bold name">Name: @item.ProductName</h5>
      </div>

      <div class="productPrice bold" id="ProductPrice">
      Rs. <span class="unit"> @item.Price</span>
      </div>
      <div class="productCart">


      <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 no-padding">
      <input type="submit" class="btn btn-success" id="btnSubmit" value="Add to Cart" />
      </div>

      </div>
      </div>

      </div>
      }
      }

      </div>
      </div>


      Here is my controller:



          [HttpPost]
      public ActionResult AddToCart(int id, FormCollection collection)
      {
      MyDBContext myDBContext = new MyDBContext();

      if (ModelState.IsValid)
      {
      var cartFill = myDBContext.Products.Find(id);

      int quantity = Convert.ToInt16(collection["Weight"]);

      AddToCart addToCart = new AddToCart(cartFill, quantity);

      if (Session["cart"] == null)
      {
      List<AddToCart> list = new List<AddToCart>();
      list.Add(addToCart);
      Session["cart"] = list;
      }
      else
      {
      List<AddToCart> list = (List<AddToCart>)Session["cart"];
      list.Add(addToCart);
      Session["cart"] = list;
      }
      }
      return RedirectToAction("ViewCart", "Home");
      }









      share|improve this question
















      This question already has an answer here:




      • Send post data from html FORM with Javascript?

        5 answers




      I am posting a selected value from Dropdown List. but after getting my value page goes refresh. How to post data without page refresh to controller?



      Here is view:



      <div class="container">
      <div class="row">
      @foreach (var item in Model)
      {
      using (Html.BeginForm("AddToCart", "Test", new { id = item.ProductId }, FormMethod.Post))
      {


      <div class="col-md-3 col-sm-4 col-xs-6">
      <img id="ImageClick" onclick="location.href='@Url.Action("ViewProductOnClick", "Home", new { id = item.ProductId })'"
      src="@Url.Content("~/Content/" + item.ImageURL)" height="200" width="200" alt="@item.ProductName" />
      <div class="productDetails">
      <div class="productName">
      <h5 id="ProductName" class="bold name">Name: @item.ProductName</h5>
      </div>

      <div class="productPrice bold" id="ProductPrice">
      Rs. <span class="unit"> @item.Price</span>
      </div>
      <div class="productCart">


      <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 no-padding">
      <input type="submit" class="btn btn-success" id="btnSubmit" value="Add to Cart" />
      </div>

      </div>
      </div>

      </div>
      }
      }

      </div>
      </div>


      Here is my controller:



          [HttpPost]
      public ActionResult AddToCart(int id, FormCollection collection)
      {
      MyDBContext myDBContext = new MyDBContext();

      if (ModelState.IsValid)
      {
      var cartFill = myDBContext.Products.Find(id);

      int quantity = Convert.ToInt16(collection["Weight"]);

      AddToCart addToCart = new AddToCart(cartFill, quantity);

      if (Session["cart"] == null)
      {
      List<AddToCart> list = new List<AddToCart>();
      list.Add(addToCart);
      Session["cart"] = list;
      }
      else
      {
      List<AddToCart> list = (List<AddToCart>)Session["cart"];
      list.Add(addToCart);
      Session["cart"] = list;
      }
      }
      return RedirectToAction("ViewCart", "Home");
      }




      This question already has an answer here:




      • Send post data from html FORM with Javascript?

        5 answers








      c# jquery asp.net-mvc-4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 14:26









      James Z

      11.1k71835




      11.1k71835










      asked Nov 15 '18 at 12:52









      Bilal MustafaBilal Mustafa

      64




      64




      marked as duplicate by user3559349 Nov 16 '18 at 0:34


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by user3559349 Nov 16 '18 at 0:34


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          You should use a partial view if you want to update only portions of the page instead of refreshing it.



          Here is a useful link to start with: http://www.tutorialsteacher.com/mvc/partial-view-in-asp.net-mvc.






          share|improve this answer





























            0














            You can do it like this;



            1) You have to create partial view for your action (Action is you want to post)



            2) Add Reference of jquery.unobtrusive-ajax.



            Using NuGet package manager, you can install library and reference into the project.
            here is the package link https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/



            3)To work Ajax.BeginForm functionality properly don't forget to add the reference into page



            After you do these, you can post data to controller without Page refresh in ASP.NET MVC using Ajax.BeginForm.



            For detailed explanation you can read this link:
            post-data-without-whole-postback






            share|improve this answer




























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You should use a partial view if you want to update only portions of the page instead of refreshing it.



              Here is a useful link to start with: http://www.tutorialsteacher.com/mvc/partial-view-in-asp.net-mvc.






              share|improve this answer


























                0














                You should use a partial view if you want to update only portions of the page instead of refreshing it.



                Here is a useful link to start with: http://www.tutorialsteacher.com/mvc/partial-view-in-asp.net-mvc.






                share|improve this answer
























                  0












                  0








                  0






                  You should use a partial view if you want to update only portions of the page instead of refreshing it.



                  Here is a useful link to start with: http://www.tutorialsteacher.com/mvc/partial-view-in-asp.net-mvc.






                  share|improve this answer












                  You should use a partial view if you want to update only portions of the page instead of refreshing it.



                  Here is a useful link to start with: http://www.tutorialsteacher.com/mvc/partial-view-in-asp.net-mvc.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 12:54









                  George FindulovGeorge Findulov

                  587411




                  587411

























                      0














                      You can do it like this;



                      1) You have to create partial view for your action (Action is you want to post)



                      2) Add Reference of jquery.unobtrusive-ajax.



                      Using NuGet package manager, you can install library and reference into the project.
                      here is the package link https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/



                      3)To work Ajax.BeginForm functionality properly don't forget to add the reference into page



                      After you do these, you can post data to controller without Page refresh in ASP.NET MVC using Ajax.BeginForm.



                      For detailed explanation you can read this link:
                      post-data-without-whole-postback






                      share|improve this answer


























                        0














                        You can do it like this;



                        1) You have to create partial view for your action (Action is you want to post)



                        2) Add Reference of jquery.unobtrusive-ajax.



                        Using NuGet package manager, you can install library and reference into the project.
                        here is the package link https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/



                        3)To work Ajax.BeginForm functionality properly don't forget to add the reference into page



                        After you do these, you can post data to controller without Page refresh in ASP.NET MVC using Ajax.BeginForm.



                        For detailed explanation you can read this link:
                        post-data-without-whole-postback






                        share|improve this answer
























                          0












                          0








                          0






                          You can do it like this;



                          1) You have to create partial view for your action (Action is you want to post)



                          2) Add Reference of jquery.unobtrusive-ajax.



                          Using NuGet package manager, you can install library and reference into the project.
                          here is the package link https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/



                          3)To work Ajax.BeginForm functionality properly don't forget to add the reference into page



                          After you do these, you can post data to controller without Page refresh in ASP.NET MVC using Ajax.BeginForm.



                          For detailed explanation you can read this link:
                          post-data-without-whole-postback






                          share|improve this answer












                          You can do it like this;



                          1) You have to create partial view for your action (Action is you want to post)



                          2) Add Reference of jquery.unobtrusive-ajax.



                          Using NuGet package manager, you can install library and reference into the project.
                          here is the package link https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/



                          3)To work Ajax.BeginForm functionality properly don't forget to add the reference into page



                          After you do these, you can post data to controller without Page refresh in ASP.NET MVC using Ajax.BeginForm.



                          For detailed explanation you can read this link:
                          post-data-without-whole-postback







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 13:42









                          OgünOgün

                          11




                          11















                              Popular posts from this blog

                              How to pass form data using jquery Ajax to insert data in database?

                              National Museum of Racing and Hall of Fame

                              Guess what letter conforming each word