C# html助手don';t在ajax.begin之后从控制器获取值
我有一个ajax.beg通知,我必须上传一个文件,然后在视图中隐藏已上传文件的路径 保存在服务器中 问题是路径的值在post调用后不会返回到视图 我正在返回带有错误的新局部视图,但该值不存在 即将到来 请帮帮我 从控制器返回部分视图的post方法C# html助手don';t在ajax.begin之后从控制器获取值,c#,asp.net-mvc,asp.net-mvc-partialview,ajax.beginform,C#,Asp.net Mvc,Asp.net Mvc Partialview,Ajax.beginform,我有一个ajax.beg通知,我必须上传一个文件,然后在视图中隐藏已上传文件的路径 保存在服务器中 问题是路径的值在post调用后不会返回到视图 我正在返回带有错误的新局部视图,但该值不存在 即将到来 请帮帮我 从控制器返回部分视图的post方法 [HttpPost] [ValidateAntiForgeryToken] public ActionResult SaveSocioDetails(SpSocio socio) // ajax for 1 step in socio
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveSocioDetails(SpSocio socio) // ajax for 1 step in socio
{
bool sociook = false;
socio.StudentId = sStudentId; // bind student id to socio model
socio = SocioDetValid(ref sociook,socio);
// add validation
if (ModelState.IsValid && sociook)
{
socio = SaveSocioModel(socio);
Response.StatusCode = 200;
}
else
Response.StatusCode = 300; // return error to client the model is not valid
return PartialView("~/Views/Student/Socio/SocioDetails.cshtml", socio); // return the partial view of the forn with validation messages
}
保存文件并将路径添加到路径字段的有效函数:
// bank account validation and save
if (socio.FileBankAccount == null) // if there is no file
{
if (socio.PathBankAccount == null) // check if he upload file already - if not add error message
{
ModelState.AddModelError("FileBankAccount", "חובה לצרף קובץ");
ok = false;
}
}
else // upload new the file
socio.PathBankAccount = Files.SaveFileInServer(socio.FileBankAccount, "BankAccount", sStudentId, socio.PathBankAccount);
视图中用于上载的部分和路径字符串的隐藏部分:
<div class="row">
<div class="col-xl-3 col-lg-3 col-md-4 col-12 ">
@Html.LabelFor(model => model.BankStatus, htmlAttributes: new { @class = "control-label col-12" })
@Html.EditorFor(model => model.BankStatus, new { htmlAttributes = new { @class = "form-control must m-1 mt-0" } })
@Html.ValidationMessageFor(model => model.BankStatus, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PathBankAccount)
</div>
<div class="col-xl-3 col-lg-3 col-md-4 col-12">
@Html.LabelFor(model => model.FileBankAccount, htmlAttributes: new { @class = "control-label col-12 must-sign", @for = "" })
<div class="chose-file m-1 mt-0">
@Html.TextBoxFor(model => model.FileBankAccount, new { @class = "form-control must", @type = "file", @accept = "image/jpeg,image/jpg,image/png,application/pdf", @style = "display:none;" })
<label for="FileBankAccount">
<i class="ml-1 material-icons">add_photo_alternate</i>
בחר קובץ
</label>
</div>
@Html.ValidationMessageFor(model => model.FileBankAccount, "", new { @class = "text-danger" })
</div>
@*
<div class="col-xl-3 col-lg-3 col-md-4 col-12" style="display:@(Model.PathBankAccount != null ? "" : "none")">
<label class="control-label col-12" for="">קובץ שמור</label>
<a name="@Model.PathBankAccount" class="btn btn-light btn-file m-1 mt-0">צפייה בקובץ שמור</a>
</div>*@
@LabelFor(model=>model.BankStatus,htmlAttributes:new{@class=“controllabel col-12”})
@EditorFor(model=>model.BankStatus,new{htmlAttributes=new{@class=“form control must m-1 mt-0”})
@Html.ValidationMessageFor(model=>model.BankStatus,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.PathBankAccount)
@LabelFor(model=>model.FileBankAccount,htmlAttributes:new{@class=“control label col-12 must sign”,@for=”“})
@Html.TextBoxFor(model=>model.FileBankAccount,新{@class=“form control must”、@type=“file”、@accept=“image/jpeg、image/jpg、image/png、application/pdf”、@style=“display:none;”
添加照片
בחר קובץ
@Html.ValidationMessageFor(model=>model.FileBankAccount,“,new{@class=“text danger”})
@*
קובץ שמור
谢谢你的帮助
更新:
Ajax表单代码:这是Ajax在大视图中的一部分
@使用(Ajax.BeginForm(“savesocialdetails”、“Student”、新的AjaxOptions{HttpMethod=“POST”、OnSuccess=“firstsuccess”、OnFailure=“socialdetailsfail”、UpdateTargetId=“partialsocio”、LoadingElementId=“div_loading”},新的{@enctype=“multipart/form data”})
{
@Html.AntiForgeryToken()
@Action(“partialSoCodetails”,“Student”,new{SpId=ViewBag.SpId})
המשך
}
这是fail and success ajax的功能:
$(文档).ready(函数()
{
});
函数firstsuccess(){
log(“这是ajaxSuccess”);
$(“#下一步”)。单击();
}
函数SocialDetailsFail(bdata){
log('这是ajaxfail');
console.log(bdata.responseText);
$('partialsocio').html(bdata.responseText);
}
在返回给客户端的脚本中,字符串的值不会出现
谢谢你的帮助
你能添加ajax表单代码吗?嗨,我添加了moe代码
<fieldset>
@using (Ajax.BeginForm("SaveSocioDetails", "Student", new AjaxOptions { HttpMethod = "POST", OnSuccess = "firstsuccess",OnFailure = "sociodetailsfail", UpdateTargetId="partialsocio" ,LoadingElementId = "div_loading" }, new { @enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div id="partialsocio">
@Html.Action("PartialSocioDetails", "Student", new { SpId = ViewBag.SpId })
</div>
<div id="div_loading" style="display:none;">
<img src="@Url.Content("~/Content/Pic/Spinner.gif")" alt="" />
</div>
<button class="btn btn-primary" type="submit">המשך</button>
}
<input type="button" name="next" class="next action-button bg-primary" hidden value="Next" id="sociodetnext" />
</fieldset>
<script>
$(document).ready(function ()
{
});
function firstsuccess() {
console.log('this is ajaxSuccess');
$("#sociodetnext").click();
}
function sociodetailsfail(bdata) {
console.log('this is ajaxfail');
console.log(bdata.responseText);
$('#partialsocio').html(bdata.responseText);
}
</script>