我正在通过mvc中的FormMethod.Post方法创建一个反馈表单。现在,在我的表单中数据正在保存,但保存后数据页面将刷新,以便用户无法知道数据是否已成功插入。所以,为此我为什么要提醒javascript并刷新当前页面。
我做了很多事,但没有做任何事情。
所以,让我告诉你我在这段代码中尝试过的最后一件事。
这是我的观点
<script>
@ViewBag.scripCall
function Callback() {
alert("Callback function is working");
}
</script>
@using (Html.BeginForm("Create", "FeedBack", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="container">
<div class="row">
<div class="col-sm-3">
<img src="~/images/default.png" style="margin:10px" height="200" width="200" id="imagePreview" />
<input type="file" name="ImageUpload" accept="image/jpeg, image/png" id="ImageUpload"
onchange="ShowImagePreview(this, document.getElementById('imagePreview'))" class="control-label" />
</div>
<div class="col-sm-2">
@Html.LabelFor(model => model.FullName, new { @class = "control-label" })
</div>
<div class="col-sm-8">
@Html.TextBoxFor(model => model.FullName, new { @class = "form-control", placeholder = "Enter The Name" })
</div>
<div class="col-sm-9">
<br />
</div>
<div class="col-sm-1">
@Html.LabelFor(model => model.Occupation, new { @class = "control-label" })
</div>
<div class="col-sm-8">
@Html.DropDownList("Occupation", ViewData["occupationList"] as SelectList, new {@class="control-label", style="width:250px;" })
</div>
<div class="col-sm-9">
<br />
</div>
<div class="col-sm-3">
@Html.LabelFor(model => model.UserFeedBack, new { @class = "control-label" })
</div>
<div class="col-sm-8">
@Html.TextBoxFor(model => model.UserFeedBack, new { @class = "form-control", placeholder = "Enter FeedBack", style="height:250px;" })
</div>
<div class="col-sm-3">
<input type="submit" id="btn_save" class="button" value="Save" />
</div>
</div>
<div class="row">
<span class="control-label" style="color:red">Note : Please do not upload <b>DSLR photo</b>. It does not support. Thank You...!!!</span>
</div>
</div>
}
这是我的控制器
public ActionResult Create(FeedBackViewModel feedbackViewModel)
{
try
{
HttpPostedFileBase file = Request.Files["ImageUpload"];
if (file != null)
{
string fileName = Path.GetFileNameWithoutExtension(feedbackViewModel.ImageUpload.FileName);
string extension = Path.GetExtension(feedbackViewModel.ImageUpload.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
feedbackViewModel.ImageUpload.SaveAs(Path.Combine(Server.MapPath("~/images/"), fileName));
}
ContentRepository service = new ContentRepository();
int i = service.CreateFeedBack(file, feedbackViewModel);
if (i == 1)
{
ViewBag.scripCall = "Callback();";
return View();
//return Content("<script language='javascript' type='text/javascript'>alert('Thanks for Feedback!');</script>");
}
else
{
return Content("<script language='javascript' type='text/javascript'>alert('Sorry, Something went wrong. Please Try Again...!!!');</script>");
}
}
catch (Exception ex)
{
return View();//"Error", new HandleErrorInfo(ex, "StaffRegistration", "Create"));
}
}
现在,在这段代码中我尝试通过@ ViewBag.scripCall显示警报的最新内容以及刷新当前页面但它也无法正常工作。
谢谢。