Hello Everyone,
I am not a professional in MVC and I get stuck in a problem from the past two days.
I have created a WCF service application in which I created [OperationContract] methods.
And in my MVC application I created a login page in which when user click on the Login button, it sends the username and password using Jquery ajax post through the link to the WCF method where the user gets authenticated and in response the WCF service sends the user about his preference data in Json format. And in success I got that data in Json. And after that I call the $.post method to other Controller action and send this Json data to it. Then I successfully get this json data in my controller action and also get back the same data after casting. But when I give this data to the view as a model it doesn't redirecting me to this controller action.
Here is the code for the login view:
@model OAS.Models.LoginModel
@{
ViewBag.Title = "Log in";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
</hgroup>
<script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var GetAuthenticateUserURl = "http://localhost:15000/AuthenticateUser";
$("#Login").click(function () {
var username = document.getElementById('username').value;
if (username == "") {
alert("Enter username");
return;
}
var password = document.getElementById('password').value;
if (password == "") {
alert("Enter password");
return;
}
$.ajax({
cache: false,
type: "GET",
async: false,
url: GetAuthenticateUserURl + "/" + username + "/" + password,
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data != null) {
alert("success");
var result = '@Url.Action("Index", "UserPreference")';
$.post(result, { "userPreferenceString": data },
function (result) {
alert("data sent");
});
}
else {
alert("Login credentials are not correct.");
}
},
error: function (xhr) {
alert("Login credentials are not correct.");
}
});
});
});
</script>
<section id="loginForm">
<h2>Use a local account to log in.</h2>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { @id = "username" })
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { @id = "password" })
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="button" value="Log in" id="Login" />
</fieldset>
<p>
@Html.ActionLink("Register", "Register") if you don't have an account.
</p>
}
</section>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
And the controller action code :
public ActionResult Index(string userPreferenceString)
{
List<UserPreferenceData> userPreferenceData=null;
if (userPreferenceString != null)
{
userPreferenceData = JsonHelper.JsonDeserialize<List<UserPreferenceData>>(userPreferenceString);
}
return View(userPreferenceData);
}
Hope you understand my question.
Thanks in advance.