【发布时间】:2015-07-02 23:28:57
【问题描述】:
我有一个问题,我想使用 C# MVC AJAX 在我的数据库中添加一个新行,但它不起作用。我可以看到文本框的值作为 URL 中的参数,但是当我单击提交按钮时,它不起作用,我不知道如何继续。你能帮我解决这个问题吗?这是代码:
我在Controller中包含插入指令的方法:
public void AddCategorie(string cat)
{
Category cate = new Category();
cate.CategoryNom = cat;
db.Categories.Add(cate);
db.SaveChanges();
}
我在视图中显示结果的代码:
@{
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>
索引中的代码(也许它可以提供帮助)它显示了应用程序的主页:
@{
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>
【问题讨论】:
-
我没有看到您的 AJAX 调用。你能发一下吗?或者也许我错过了什么......
-
你的意思是这样的:$.getJSON(......... Daniel?
-
现在看起来像这样:function AddCategorie(categorie) { $.getJSON("Animes/AddCategorie", { cat: categorie }, function (data) { }) 我只是不知道大括号之间放什么
-
没错!我没有看到 POST 到 ~/AddCategorie 的调用,数据是否正确传递给方法?
-
对于不会查看 cmets 的任何人,请将其添加到原始帖子 Joel =) 这也是正确的语法,您在哪里/如何调用您的函数 AddCategorie()?您可以手动点击该函数并查看它是否正在将数据传递给您的控制器吗?