I am supposed to convert a form submission from MVC Razor submission to an Ajax submission but I am having trouble even using simple javascript functions in the view of the form (Add.cshtml).
For starters, I try to link the JS file that i want to use in the following way:
@section JavaScript
{
<script type="text/javascript" src="@Url.Content("/Scripts/ajax_submit.js")"></script>
}
The javascript file looks like this:
function dosubmit() {
$("#add_address").ajaxForm({ url: '~/Home/Add', type: 'post' })
alert("IT WORKS");
return false;// if it's a link to prevent post
}
and I have my form built in the following way:
@model MvcApplicationTest.Models.PersonModel
@section JavaScript
{
<script type="text/javascript" src="@Url.Content("/Scripts/ajax_submit.js")"></script>
}
@{
ViewBag.Title = "Hinzufügen";
}
<h2>Hinzufügen</h2>
<form id="add_address">
<div class="fieldrow">
@Html.LabelFor(x => x.Nachname)
@Html.TextBoxFor(x => x.Nachname)
@Html.ValidationMessageFor(x => x.Nachname)
</div>
<div class="fieldrow">
@Html.LabelFor(x => x.Vorname)
@Html.TextBoxFor(x => x.Vorname)
@Html.ValidationMessageFor(x => x.Vorname)
</div>
<div class="fieldrow">
@Html.LabelFor(x => x.Strasse)
@Html.TextBoxFor(x => x.Strasse)
@Html.ValidationMessageFor(x => x.Strasse)
</div>
<div class="fieldrow">
@Html.LabelFor(x => x.Hausnummer)
@Html.TextBoxFor(x => x.Hausnummer)
@Html.ValidationMessageFor(x => x.Hausnummer)
</div>
<div class="fieldrow">
@Html.LabelFor(x => x.Plz)
@Html.TextBoxFor(x => x.Plz)
@Html.ValidationMessageFor(x => x.Plz)
</div>
<div class="fieldrow">
@Html.LabelFor(x => x.Ort)
@Html.TextBoxFor(x => x.Ort)
@Html.ValidationMessageFor(x => x.Ort)
</div>
<!--Martin-->
<div class="fieldrow">
@Html.LabelFor(x => x.Email)
@Html.TextBoxFor(x => x.Email)
@Html.ValidationMessageFor(x => x.Email)
</div>
<input type="button" onclick="dosubmit()" value="Speichern" />
</form>
However, when I click the button nothing happens (it should pop up a window with the desired text).
I have tried to include the JS file into the _Layout.cshtml, but it still doesn't work.
Thanks for your help :)