I have a problem, I want to add a new row into my database using C# MVC AJAX and it doesn't work. I can see the value of the textbox as a parameter in the URL, but when I click on the submit button, it doesn't work and I don't know how to proceed. Can you please help me to solve this problem? Here's the codes :
My method in the Controller who contains the insert instruction:
public void AddCategorie(string cat)
{
Category cate = new Category();
cate.CategoryNom = cat;
db.Categories.Add(cate);
db.SaveChanges();
}
My code in the View who's showing the result :
@{
ViewBag.Title = "Gestion des catégories";
}
<hgroup class="title">
<h1>@ViewBag.Title</h1>
</hgroup>
<section class="contact">
<form ng-model="AddCategorie()">
<input type="text" name="name" placeholder="Nouvelle catégorie" /><br />
<input type="submit" value="Ajouter" />
</form>
<input type="submit" value="Modifier" />
<input type="submit" value="Supprimer" />
</section>
The code in the Index (maybe it can help) It's showing the main page of the application:
@{
ViewBag.Title = "Home Page";
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0-alpha.4/handlebars.min.js"></script>
<script src="http://bootswatch.com/spacelab/bootstrap.min.css"></script>
@*<script src="jquery.min.js"></script>
<script src="jssor.slider.mini.js"></script>
<script>
jQuery(document).ready(function ($) {
var options = { $AutoPlay: true };
var jssor_slider1 = new $JssorSlider$('slider1_container', options);
});
</script>*@
<style>
.categories, .animes, .perso {
color: #000;
margin-left: 35px;
}
</style>
<script>
templateCategories = null;
templateAnimes = null;
templatePerso = null;
$(function () {
templateCategories = Handlebars.compile($('#CategoriesTemplate').html());
templateAnimes = Handlebars.compile($('#AnimesTemplate').html());
templatePerso = Handlebars.compile($('#PersoTemplate').html());
$.getJSON("Animes/GetCategories", null, function (data) {
var result = templateCategories(data);
$('#CategoriesOutput').html(result);
});
});
function GetAnimes(catID) {
$.getJSON("Animes/GetAnimes", { categoryID: catID }, function (data) {
var resAnimes = templateAnimes(data);
$('#AnimesOutput').html(resAnimes);
$('lstCat').html(resAnimes);
});
}
function GetPersonnages(aniID) {
$.getJSON("Animes/GetPersonnages", { animeID: aniID }, function (data) {
var resPerso = templatePerso(data);
$('#PersoOutput').html(resPerso);
$('#lstAni').html(resPerso);
});
}
function AddCategorie(categorie) {
$.getJSON("Animes/AddCategorie", { cat: categorie }, function (data) {
});
}
</script>
<h2>Slideshow</h2>
<div id="slider1_container" style="position: relative; top: 0px; left: 0px; width: 600px; height: 300px;">
<!-- Slides Container -->
<div u="slides" style="cursor: move; position: absolute; overflow: hidden; left: 0px; top: 0px; width: 600px; height: 300px;">
<div><img src="../imgs/elfen1.jpg" /></div>
</div>
</div>
<h3>Catégories</h3>
<div id="CategoriesOutput">
</div>
<h3>Animes</h3>
<div id="AnimesOutput">
</div>
<h3>Personnages</h3>
<div id="PersoOutput">
</div>
<script id="CategoriesTemplate" type="text/x-handlebars-template">
{{#each}}
<h3 class="categories" onclick="GetAnimes({{CategoryID}})">{{CategoryNom}}</h3>
{{/each}}
</script>
<script id="AnimesTemplate" type="text/x-handlebars-template">
{{#each}}
<h3 class="animes" onclick="GetPersonnages({{AnimeID}})">{{AnimeNom}}</h3>
{{/each}}
</script>
<script id="PersoTemplate" type="text/x-handlebars-template">
{{#each}}
<h3 class="perso">{{PersonnageNom}}</h3>
{{/each}}
</script>