如何从 javascript - ASP .Net MVC C# 调用 C# class 的无效方法
How to call a void method of a C# class from javascript - ASP .Net MVC C#
我需要你的帮助来解决我无法解决的问题:)
我的程序中有这个表格来上传文件
@using(Html.BeginForm("UploadDocuments", "User", FormMethod.Post, new { enctype = "multipart/form-data", id = "form-upload-documents" }))
{
@Html.AntiForgeryToken()
<ul id="UploadContainer" class="upload-container" data-last-id="0">
<li class="col-upload-figure">
<figure class="upload-figure">
<label class="btn-upload" for="Picture_0">
<span class="btn-upload-text">Add a picture</span>
</label>
<input class="custom-input-file" data-id="0" id="Picture_0" name="Picture_0" type="file">
</figure>
</li>
</ul>
<div class="form-submit">
<button class="btn btn-primary btn-loader" data-style="expand-right" type="submit">Submit</button>
</div>
}
然后我用javascript检查我要上传的文件的大小,如果文件太大,我想调用一个C#的void方法class而不离开当前页面
$('#Picture_0').change(function() {
var files = $(this).prop('files');
var fullPath = $(this).val();
if (files[0].size > 4*1024*1024) {
$('#Picture_0').val('')*;
// The function of a specific class I would like to call
}
else{
//...
}
})
我该如何执行?
一个 ajax 调用可以解决这个问题。类似于:
$.ajax({
url:'@Url.Action("Action", "controller")',
type: 'POST',
data: //Your data, //Possibly a json
contentType: 'Application/json',
success: function(result)
{
}
});
你可以通过ajax,
调用
public ActionResult MethodName()
{
// your logic
EmptyResult e = new EmptyResult();
return e;
}
好吧,如果它在控制器中,那么它应该具有 return 类型的 ActionResult。
但是如果你想调用一个函数,那么你应该使用 webapi。
我需要你的帮助来解决我无法解决的问题:)
我的程序中有这个表格来上传文件
@using(Html.BeginForm("UploadDocuments", "User", FormMethod.Post, new { enctype = "multipart/form-data", id = "form-upload-documents" }))
{
@Html.AntiForgeryToken()
<ul id="UploadContainer" class="upload-container" data-last-id="0">
<li class="col-upload-figure">
<figure class="upload-figure">
<label class="btn-upload" for="Picture_0">
<span class="btn-upload-text">Add a picture</span>
</label>
<input class="custom-input-file" data-id="0" id="Picture_0" name="Picture_0" type="file">
</figure>
</li>
</ul>
<div class="form-submit">
<button class="btn btn-primary btn-loader" data-style="expand-right" type="submit">Submit</button>
</div>
}
然后我用javascript检查我要上传的文件的大小,如果文件太大,我想调用一个C#的void方法class而不离开当前页面
$('#Picture_0').change(function() {
var files = $(this).prop('files');
var fullPath = $(this).val();
if (files[0].size > 4*1024*1024) {
$('#Picture_0').val('')*;
// The function of a specific class I would like to call
}
else{
//...
}
})
我该如何执行?
一个 ajax 调用可以解决这个问题。类似于:
$.ajax({
url:'@Url.Action("Action", "controller")',
type: 'POST',
data: //Your data, //Possibly a json
contentType: 'Application/json',
success: function(result)
{
}
});
你可以通过ajax,
调用public ActionResult MethodName()
{
// your logic
EmptyResult e = new EmptyResult();
return e;
}
好吧,如果它在控制器中,那么它应该具有 return 类型的 ActionResult。
但是如果你想调用一个函数,那么你应该使用 webapi。