Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :
XML
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.
Posted
Updated 12-Jun-14 20:46pm
v2

1 solution

Hi,

You need to pass the "userPreferenceData" as return view(userPreferenceData) and also on the view side you need to bind it to a model or else you can take this "userPreferenceData" into tempdata and then use it on the view.

Hope it helps.

Regards,
Nirmal
 
Share this answer
 
Comments
leonidasvijay 13-Jun-14 8:57am    
Thanks for reply.

I have already done the same thing as you said.
I think the controller code in my question wasn't posted properly.
Here is the complete controller code :
public ActionResult Index(string userPreferenceString)
{
List<userpreferencedata> userPreferenceData = null;
if (userPreferenceString != null)
{
userPreferenceData = JsonHelper.JsonDeserialize<List<userpreferencedata>>(userPreferenceString);
}
return View(userPreferenceData);
}

And using this code, it is giving me all the objects in the list and even it set the model data in the view. But it fails to redirect to the view. It always stuck on the same page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900