【问题标题】:Passing Model from view to controller using Jquery Ajax使用 Jquery Ajax 将模型从视图传递到控制器
【发布时间】:2013-05-31 09:20:55
【问题描述】:

我想将我的 ViewModel 从 View 传递给 Controller,因为我正在使用 Ajax,我的代码如下,我必须显示警报框,我收到一个错误 “无效的 JSON 原语:信息。”

控制器:

[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 {

    [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;}


}

}

现在我想将我的 ViewModel 从视图传递到控制器。 提前致谢!!!

【问题讨论】:

标签: jquery ajax asp.net-mvc json


【解决方案1】:

改变

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

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

另见this question

【讨论】:

  • 这对我有用,但我必须显示“return Json(new{success=true});”和“return Json(new { error = true, message = "Ftp Test Failed : " + ex.Message })" 在我的警报框中
  • 那么,这有什么问题?
  • 我必须在第一次返回时显示“测试通过”,在第二次返回时显示“Ftp 测试失败”
  • @SantoshMishra 你不应该使用alert() 进行调试。这不是很明显吗?您正在将一个对象转换为字符串,它通常看起来像“[object][object]”。您可以使用浏览器中的调试工具,也可以使用alert() 要查看的对象的属性(例如:alert(data.FtpPath);)。
  • 所以你想说我必须替换“alert(data);”带有“警报(data.FtpPath);”???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 2012-01-19
  • 2013-06-03
  • 1970-01-01
相关资源
最近更新 更多