Asp.net MVC Post Data To Controller without refreshing page [duplicate]
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");
}
c# jquery asp.net-mvc-4
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.
add a comment |
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");
}
c# jquery asp.net-mvc-4
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.
add a comment |
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");
}
c# jquery asp.net-mvc-4
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
c# jquery asp.net-mvc-4
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.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 '18 at 12:54
George FindulovGeorge Findulov
587411
587411
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 13:42
OgünOgün
11
11
add a comment |
add a comment |