Skip to content

Commit

Permalink
able to create things with ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
Caitlin Hines and Tammy Dang committed May 4, 2017
1 parent 35cab4e commit cb3814b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 28 deletions.
Binary file modified .vs/Shoes/v14/.suo
Binary file not shown.
9 changes: 5 additions & 4 deletions src/Shoes/Controllers/SalesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public IActionResult Create()
}

[HttpPost]
public IActionResult Create(Sale sale)
public IActionResult Create(string newShoe, int newPrice, string newImage, string newComment)
{
db.Sales.Add(sale);
db.SaveChanges();
return RedirectToAction("Index", "Sales");
Sale newSale = new Sale(newShoe, newPrice, newImage, newComment);
db.Sales.Add(newSale);
db.SaveChanges();
return Json(newSale);
}

public IActionResult Edit(int id)
Expand Down
16 changes: 16 additions & 0 deletions src/Shoes/Models/Sale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Shoes.Models
{
[Table("Sales")]
public class Sale
{
[Key]
Expand All @@ -15,5 +17,19 @@ public class Sale
public string Image { get; set; }
public string Comment { get; set; }
public virtual Inventory Inventory { get; set; }

public Sale()
{

}

public Sale(string _shoename, int _price, string _image, string _comment, int _id = 0)
{
ShoeName = _shoename;
Price = _price;
Image = _image;
Comment = _comment;
}

}
}
40 changes: 20 additions & 20 deletions src/Shoes/Views/Sales/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@

<h3>Add A Product</h3>

<div class="container">
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.ShoeName)
@Html.EditorFor(model => model.ShoeName, new { htmlAttributes = new { @class = "form-control" } })
<div class="newcontainer">

<form action="Create" class="new-sale">
<label for="newShoe">Shoe:</label>
<input type="text" name="newShoe" />
<br />
<label for="newPrice">Price</label>
<input type="text" name="newPrice" />
<br />
<label for="newImage">Image</label>
<input type="text" name="newImage" />
<br />
<label for="newComment">Comment</label>
<input type="text" name="newComment" />
<br />

<button type="submit">Submit</button>
</form>

</div>

@Html.LabelFor(model => model.Price)
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })

@Html.LabelFor(model => model.Image)
@Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } })

@Html.LabelFor(model => model.Comment)
@Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })

<input type="submit" value="Add Product" class="btn btn-default" />

}

<p>@Html.ActionLink("Back to Sales", "Index", "Sales")</p>
</div>
7 changes: 6 additions & 1 deletion src/Shoes/Views/Sales/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
</p>
}

<div class="details"></div>
<p class="newShoe"></p>

<div class="click-create">Add shoe to inventory</div>

<div class="details"></div>
<div class="create"></div>
Binary file modified src/Shoes/bin/Debug/netcoreapp1.0/Shoes.dll
Binary file not shown.
Binary file modified src/Shoes/bin/Debug/netcoreapp1.0/Shoes.pdb
Binary file not shown.
32 changes: 29 additions & 3 deletions src/Shoes/wwwroot/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
type: 'GET',
datatype: 'json',
contentType: 'application/json',
url:$(this).data('request-url'),
url: $(this).data('request-url'),
success: function (result) {
console.log(result);
var resultString = 'Id: ' + result.id + '<br>Stock: ' + result.stock + '<br>Revenue: ' + result.revenue;
var resultString = 'Id: ' + result.id + '<br>Stock: ' + result.stock + '<br>Revenue: ' + result.revenue;
$('#result').html(resultString);
}
});
Expand All @@ -23,4 +23,30 @@
}
})
})
});
$('.click-create').click(function () {
$.ajax({
type: 'GET',
datatype: 'html',
url: "Sales/Create",
success: function (result) {
console.log(result);
$('.create').html(result);
}
});
});
$('.new-sale').submit(function (event) {
event.preventDefault();
alert("this works!")
$.ajax({
type: 'POST',
dataType: 'json',
url: 'Sales/Create',
data: $(this).serialize(),
success: function (result) {
var returnResult = result.ShoeName;
$('.newShoe').append('<p>' + returnResult + '</p>');
}
});
});

});

0 comments on commit cb3814b

Please sign in to comment.