gpt4 book ai didi

jquery - 使用 Jquery Ajax 将模型从 View 传递到 Controller

转载 作者:行者123 更新时间:2023-12-01 01:15:46 25 4
gpt4 key购买 nike

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

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

查看:

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

型号:

公共(public)分部类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 从 View 传递到 Controller 。提前致谢!!!

最佳答案

改变

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

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

另请参阅this question .

关于jquery - 使用 Jquery Ajax 将模型从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16853941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com