3

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."

Controller:

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

View:

@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>

Model:

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!!!

Santosh Mishra
  • 33
  • 1
  • 1
  • 5
  • possible duplicate of [Invalid JSON primitive: id](http://stackoverflow.com/questions/7830961/invalid-json-primitive-id) – CodeCaster May 31 '13 at 09:56

1 Answers1

3

Change

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

To

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

See also this question.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • this work for me but i have to show the "return Json(new{ success=true});" and "return Json(new { error = true, message = "Ftp Test Failed : " + ex.Message })" in my alert box – Santosh Mishra May 31 '13 at 10:12
  • So, what is the problem with that? – CodeCaster May 31 '13 at 10:14
  • i have to show on first return that "Test is passed" and on second return "Ftp Test Failed" – Santosh Mishra May 31 '13 at 10:15
  • @SantoshMishra you shouldn't use `alert()` for debugging. Isn't it obvious? You're converting an object to string, which usually looks like "[object][object]". You can either use debugging tools from your browser, or `alert()` the properties of the object you want to see (say: `alert(data.FtpPath);`). – CodeCaster May 31 '13 at 10:30
  • so you want to say that i have to replace "alert(data);" with "alert(data.FtpPath);"??? – Santosh Mishra May 31 '13 at 10:32
  • @SantoshMishra I did help you out, I gave you some pointers on what you can try. Then do try that, don't wait for me to spell it out. :-) – CodeCaster May 31 '13 at 11:09