I am not getting why my code is not working. I am trying to validate a code by calling a function in controller. if this code already exists i simply sent 'failed' text and then i want the form to stop its submission. but this does not work. even i get failed error my forms does not stopping itself by getting submitted.
here is the code for view
Script
$(document).ready(function () {
$('#signupform').submit(function () {
var code = $('#txtcode').val();
alert(code);
if (code.length > 0) {
//validate email
$.ajax({
type: "POST",
url: "@(Url.Action("CheckCode", "Home"))",
data: {
"code": code,
"type": "Data"
},
success: function (returndata) {
if (returndata.match('failed')) {
alert(returndata);
return false;
}
}
});
}
});
});
Form
@using (Html.BeginForm("ManipuleInfo", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "signupform", name = "signupform" }))
{
<div>
<table>
<tr>
<td>
Code:
</td>
<td>
@Html.HiddenFor(m => m.Id)
@Html.TextBoxFor(m => m.Code, new { @id = "txtcode", @name = "txtcode", @required = "required" })
</td>
</tr>
<tr>
<td>
Title:
</td>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
</tr>
<tr>
<td>
Sub- Type:
</td>
<td>
@Html.DropDownListFor(m => m.SubType, listItemsmode)
</td>
</tr>
<tr>
<td>
Subscriber Type:
</td>
<td>
@Html.DropDownListFor(m => m.SubscriberType, listItemstypes)
</td>
</tr>
<tr>
<td>
<label> Live:</label>
</td>
<td>
@Html.CheckBoxFor(m => m.Published)
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<label> Press add button to start adding fields!</label>
<table id="Controls" style="display: none"></table>
</td>
</tr>
<tr>
<td></td>
<td>
<div style="text-align:right">
<input type="submit" value="Save" class="btnStyle" name="btnSaveData" />
<input type="button" value="Cancel" onclick="return Cancel()" class="btnStyle" />
</div>
</td>
</tr>
</table>
</div>
}
Controller
public ActionResult CheckCode(string type, string code)
{
try
{
WCMSDataContext wcmsContext = new WCMSDataContext();
if (type == "Data")
{
var Objp = from p in wcmsContext.Packages.Where(p => p.Code == code && p.Type == "Data") select p;
if (Objp.Count() > 0)
{
return Json("failed");
}
}
return Json("success");
}
catch (Exception ex)
{
throw ex;
}
}