传递模型从视图中使用jQuery的Ajax到控制器 [英] Passing Model from view to controller using Jquery Ajax

查看:164
本文介绍了传递模型从视图中使用jQuery的Ajax到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从查看通过我的视图模型的控制器此我使用Ajax和我的code是如下中,我必须表明的警告框,我得到一个错误的 无效的JSON原始:信息

I want to pass my ViewModel from View to the Controller for this i am using Ajax and my code is as follows in which i have to show the alert box and i am getting an error as "Invalid JSON primitive: info."

控制器:

[HttpPost]
        public JsonResult Test(OData.FtpAccount info) {
            try {
                string FileName = Utils.File.TempName + ".txt";

                FtpClient ftp = GetClient(info);

                UnicodeEncoding uni = new UnicodeEncoding();
                byte[] guid = uni.GetBytes(Utils.File.TempName);

                FileName = info.Root + (info.Root.EndsWith("/") ? "" : "/") + FileName;
                ftp.Upload(GetTempFile(guid),FileName); //Upload File to Ftp in FtpPath Directory.

                string url = info.GetHttpUrl(FileName);
                byte[] result = Utils.Web.ReadByte(new System.Uri(url));

                ftp.FtpDelete(FileName);

                if (uni.GetString(result) == uni.GetString(guid)) {
                    return Json(new{ success=true});
                } else {
                    return Json(new { warning = true, message = "Warning : Test Upload worked, Test Delete Worked, Http Access of File did not return same content as uploaded." }); 
                }
            } catch (System.Exception ex) {
                return Json(new { error = true, message = "Ftp Test Failed : " + ex.Message });
            }
        }

查看:

@model VZDev.ViewModels.FtpAccountViewModel
@{
    ViewBag.Title = "Watch";
    var val = Json.Encode(Model);

}
<div class="control-group">
        <div class="controls">
            <button type="button" class="btn" id="test"><i class="icon-test"></i> Test</button>
        </div>
    </div>


}
<script type="text/javascript">
    $(function () {
        $("#test").click(function () {
            var check=@Html.Raw(val);
            $.ajax({
                type: 'post',
                url: rootURL + 'Ftp/Test',
                data: {info:JSON.stringify(check)},
                contentType: 'application/json; charset=utf-8',
                dataType: "json",
                success: function (data) {
                    alert(data);
                }
            });
        });
    });

</script>

型号:

公共部分类FtpAccount     {

public partial class FtpAccount {

    [DataMember(Order = 1)]
    [ScaffoldColumn(false),DatabaseGenerated(DatabaseGeneratedOption.Identity),Key,UIHint("Id"),Display(Name="Id")]
    [Column("ID")]
    public long ID{get;set;}

    [DataMember(Order = 2)]
    [UIHint("Service Provider"),Display(Name="Service Provider"),Required(ErrorMessage="Service Provider is required"),StringLength(100)]
    [Column("ServiceProvider")]
    public string ServiceProvider{get;set;}

    [DataMember(Order = 3)]
    [UIHint("Ftp Path"),Display(Name="Ftp Path"),Required(ErrorMessage="Ftp Path is required"),StringLength(500)]
    [Column("FtpPath")]
    public string FtpPath{get;set;}


}

}

现在在这里,我想从视图控制器通过我的视图模型。 在此先感谢!

now here i want to pass my ViewModel from view to controller. Thanks in Advance!!!

推荐答案

修改

data: {info:JSON.stringify(check)}

data: '{info:' + JSON.stringify(check) + '}' 

另请参阅这个问题

这篇关于传递模型从视图中使用jQuery的Ajax到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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