将json数据传递给操作后无法返回视图 [英] Failed to return view after passing json data to action

查看:73
本文介绍了将json数据传递给操作后无法返回视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone,



我不是MVC的专业人士,过去两天我遇到了问题。



我创建了一个WCF服务应用程序,我在其中创建了[OperationContract]方法。

在我的MVC应用程序中,我创建了一个登录页面,当用户点击登录时按钮,它使用Jquery ajax post通过指向用户获得身份验证的WCF方法的链接发送用户名和密码,作为响应,WCF服务以Json格式向用户发送他的首选项数据。并且成功地我在Json中获得了这些数据。然后我将$ .post方法调用到其他Controller操作并将此Json数据发送给它。然后我成功地在我的控制器操作中获取此json数据,并在转换后获取相同的数据。但是,当我将此数据作为模型提供给视图时,它不会将我重定向到此控制器操作。



以下是登录视图的代码:

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.

推荐答案

.post方法到其他Controller操作并将此Json数据发送给它。然后我成功地在我的控制器操作中获取此json数据,并在转换后获取相同的数据。但是,当我将此数据作为模型提供给视图时,它不会将我重定向到此控制器操作。



以下是登录视图的代码:

.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;
(document).ready(function () { var GetAuthenticateUserURl = "http://localhost:15000/AuthenticateUser";


(#Login)。click(function(){
var username = document.getElementById('username')。value;
if(username ==){
alert(输入用户名);
return;
}
var password = document.getElementById('password')。值;
if(密码==){
alert(输入密码);
return;
}
("#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; }


这篇关于将json数据传递给操作后无法返回视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆