私は何時間もインターネットから何かを検索しましたが、なぜこれが機能していないのか分かりませんでした。メソッドは、AJAXからのみ呼び出すことはできません
私は、ユーザーが記入してフォームを作成し、そのフォームをデータベースに保存することを簡単にします。私は自分のアヤックスを入れる前に、完璧に働いていました。しかし、今、それはエラーを育て続ける:
// GET: /AjaxStuff/Create
public ActionResult Contact()
{
return View();
}
// POST: /AjaxStuff/Create
[HttpPost]
public ActionResult Contact(Users usertocreate)
{
if (!Request.IsAjaxRequest())
{
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
return Content("Sorry, this method can't be called only from AJAX.");
}
try
{
if (ModelState.IsValid)
{
_db.Table.Add(usertocreate);
_db.SaveChanges();
return Content("Record added successfully !");
}
else
{
StringBuilder strB = new StringBuilder(500);
foreach (ModelState modelState in ModelState.Values)
{
foreach (ModelError error in modelState.Errors)
{
strB.Append(error.ErrorMessage + ".");
}
}
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
return Content(strB.ToString());
}
}
catch (Exception ee)
{
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
return Content("Sorry, an error occured." + ee.Message);
}
}
ビュー:
@model Site01.Models.Users
@{
ViewBag.Title = "Contact";
AjaxOptions options = new AjaxOptions
{
Confirm = "Are you sure to save record?",
OnBegin = "OnBeginMethod",
OnFailure = "OnFailtureMethod",
OnSuccess = "OnSuccessMethod",
OnComplete = "OnCompleteMethod"
};
}
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<h3>Deixe-nos uma mensagem!</h3>
<p>Duvidas ou apenas a deixar um comentário? Preencha o formulário!</p>
@using (Ajax.BeginForm(options))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Idade, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Idade, new
{
htmlAttributes = new { @class = "form-control" }
})
@Html.ValidationMessageFor(model => model.Idade, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tel, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Tel)
@Html.ValidationMessageFor(model => model.Tel, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataNas, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.DataNas)
@Html.ValidationMessageFor(model => model.DataNas, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CP, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.CP)
@Html.ValidationMessageFor(model => model.CP, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Localidade, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Localidade)
@Html.ValidationMessageFor(model => model.Localidade, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Mensagem, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Mensagem)
@Html.ValidationMessageFor(model => model.Mensagem, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
<label id="labelAjaxStatus" class="alert-warning"></label>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Ver Lista", "User")
</div>
<script type="text/javascript">
var isError = false;
function OnBeginMethod() {
$("#labelAjaxStatus").text("Carregar ....");
}
function OnFailtureMethod(error) {
$("#labelAjaxStatus").text("Ocorreu um erro." + error.responseText);
isError = true;
}
function OnSuccessMethod(data) {
$("#labelAjaxStatus").text("Dados foram guardados com sucesso!");
$("#Nome").val('');
$("#Idade").val('');
$("#Email").val('');
$("#Tel").val('');
$("#DataNas").val('');
$("#CP").val('');
$("#Localidade").val('');
$("#Mensagem").val('');
}
function OnCompleteMethod(data, status) {
if (!isError) {
$("#labelAjaxStatus").text("Status do processo: " +
status);
}
}
</script>
@Scripts.Render("~/bundles/jqueryval")
はポルトガル語を気にしないでくださいSorry, this method can't be called only from AJAX.
ここに私のコード -
コントローラです。 誰かが間違っていることを知っていますか?
どのjqueryバージョンを使用していますか? – JTMon
私は1.8.0を使用しています – Firebal69