How d i post AJAX begin form data to Controller [Resolved]

Posted by Self-Innovator under ASP.NET AJAX on 4/29/2016 | Points: 10 | Views : 772 | Status : [Member] | Replies : 2
Hi All,
I want to pass Ajax begin form data to my controller i'm new to Ajax here is my code appreciate anyones help
 @using (Ajax.BeginForm("AddNewStore", "StoreRegistration", new AjaxOptions { HttpMethod = "POST", OnSuccess = "successOfAddition", OnFailure = "FailureOfAddition" }, new { id = "addStoreRegisterForm" }))
{
<form class="registration-form">
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.StoreNumber, new { @class = "form-control", placeholder = "Store Number" }) @Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreNumber)
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.FirstName, new { @class = "form-control", placeholder = "Firt Name" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.FirstName)
<input type="reset" value="Cancel" />
<input id="btnRegisterStore" type="submit" value="Agree" />
</form>
}
</div>

<script src="~/Scripts/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnRegisterStore").click(function (e) {
//var dataToPost = $("#RegisterStore").serialize()
//debugger;
$.ajax(
{
type: "POST",
data: dataToPost,
datatype: "json",
url: '@Url.Action("AddNewStore", "StoreRegistration")',
contentType: 'application/json; charset=utf-8',
success: function (result) {
redirectToLoginPage(result);
$('#unobtrusive').html(result);
PagedOnComplete();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to load the result');
}
})
})
});
</script>

My Controller
==========
public async Task<ActionResult> AddNewStore(StoreRegistrationViewModel storeRegistrationVModel)
{
var message = new MessageModel();
Convert.ToInt32(PrincipalContext.StoreNumber) : 01800;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<StoreRegistrationInfoModel, StoreRegistration>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
var storeRegistrationAccessModel = mapper.Map<StoreRegistrationInfoModel, StoreRegistration>(storeRegistrationVModel.StoreRegistrationAssociation);
var storeUser = new sp_getStoreRetailIdentities_Result
{
categoryid =storeRegistrationAccessModel.StoreIdentity,
retailname=storeRegistrationAccessModel.StoreName
};
var identityResult = await identityValidator.ValidateAsync(storeUser);
if (!identityResult.Succeeded)
{
message = GetMessages(MessageType.Error, string.Join(" ", identityResult.Errors));
return Json(message);
}
try
{
await storeRegistrationAccessRepository.SubmitStoreRegistrationAsync(storeRegistrationAccessModel);


Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Self-Innovator on: 4/29/2016 [Member] Bronze | Points: 25

Up
0
Down

Resolved

@using Newtonsoft.Json;
@model Mol2.Website.Models.StoreRegistration.StoreRegistrationViewModel
@{
ViewBag.Title = "Index";
}

<div class="container">
<div class="container login-box">
<div class="registration">
<header>
<img src="~/images/logo.jpg" />
</header>
<div class="section">
<h1>REGISTRATION</h1>
@{
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
}
@using (Ajax.BeginForm("AddNewStore", "StoreRegistration", new AjaxOptions { HttpMethod = "POST", OnSuccess = "successOfAddition", OnFailure = "FailureOfAddition" }, new { id = "addStoreRegisterForm" }))
{
<form class="registration-form">

<div class="form-group col-md-6">
<div class="form-group">
<!--[if gte IE 9]><div class="input-group"><span class="input-group-addon">Store Number</span><![endif]-->
@*<input type="text" placeholder="Store Number" value="" name="storeno" class="form-control" />*@
<!--[if gte IE 9]></div><![endif]-->
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.StoreNumber, new { @class = "form-control", placeholder = "Store Number" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreNumber)
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
<!--[if gte IE 9]><div class="input-group"><span class="input-group-addon">Store Name</span><![endif]-->
@*<input type="text" placeholder="Store Name" value="" name="storename" class="form-control" />*@
<!--[if gte IE 9]></div><![endif]-->
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.StoreName, new { @class = "form-control", placeholder = "Store Name" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreName)
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
<!--[if gte IE 9]><div class="input-group"><span class="input-group-addon">Store Address</span><![endif]-->
@*<input type="text" placeholder="Store Address" value="" name="storeaddress" class="form-control" />*@
<!--[if gte IE 9]></div><![endif]-->
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.StoreAddress.Street1, new { @class = "form-control", placeholder = "Store Address" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreAddress.Street1)
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
<!--[if gte IE 9]><div class="input-group"><span class="input-group-addon">City</span><![endif]-->
@*<input type="text" placeholder="City" value="" name="city" class="form-control" />*@
<!--[if gte IE 9]></div><![endif]-->
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.City, new { @class = "form-control", placeholder = "City" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.City)
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
<!--[if gte IE 9]><div class="input-group"><span class="input-group-addon">Store Address</span><![endif]-->
@*<input type="text" placeholder="Store Address" value="" name="storeaddress" class="form-control" />*@
<!--[if gte IE 9]></div><![endif]-->
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.StoreAddress.Zip, new { @class = "form-control", placeholder = "Zip" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreAddress.Zip)
</div>
</div>
<div class="form-group col-md-6">
<div class="form-group">
<!--[if gte IE 9]><div class="input-group"><span class="input-group-addon">Store Address</span><![endif]-->
@*<input type="text" placeholder="Store Address" value="" name="storeaddress" class="form-control" />*@
<!--[if gte IE 9]></div><![endif]-->
@Html.TextBoxFor(x => x.StoreRegistrationAssociation.StoreAddress.Country, new { @class = "form-control", placeholder = "Country" })
@Html.ValidationMessageFor(x => x.StoreRegistrationAssociation.StoreAddress.Country)
</div>
</div>
<div class="form-group co

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saki on: 4/29/2016 [Member] Starter | Points: 25

Up
0
Down
wow its great message using ajax. i have get more clarification. thanks for your forum information.

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response