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 [email protected](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!!!

1

1 Answer 1

3

Change

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

To

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

See also this question.

Sign up to request clarification or add additional context in comments.

6 Comments

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
So, what is the problem with that?
i have to show on first return that "Test is passed" and on second return "Ftp Test Failed"
@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);).
so you want to say that i have to replace "alert(data);" with "alert(data.FtpPath);"???
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.