Jquery MVC5部分视图重定向而不是更新DIV
我不止一次地发现这个问题,但我没有找到适合我的解决方案 我有一个看法:Jquery MVC5部分视图重定向而不是更新DIV,jquery,asp.net,asp.net-mvc-5,asp.net-ajax,Jquery,Asp.net,Asp.net Mvc 5,Asp.net Ajax,我不止一次地发现这个问题,但我没有找到适合我的解决方案 我有一个看法: <div id="profpics"> @Html.Partial("_ProfileImagesPartial", Model.ProfileImages) @using (Ajax.BeginForm("UploadImage", "ProfileImages", new AjaxOptions { UpdateTargetId = "profpics", HttpMethod = "GET
<div id="profpics">
@Html.Partial("_ProfileImagesPartial", Model.ProfileImages)
@using (Ajax.BeginForm("UploadImage", "ProfileImages", new AjaxOptions { UpdateTargetId = "profpics", HttpMethod = "GET" }))
{
@Html.AntiForgeryToken()
<input type="file" name="file" id="imgInp" />
<img id="blah" src="#" alt="your image" />
<input type="submit" value="OK" />
}
</div>
在my_Layout.cshtml中,并更新了jqueryval以包含不引人注目的验证:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
我还尝试手动添加它们:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
但这也没用
有人知道会是什么吗
编辑:我发现我的ajax表单没有真正工作。我用ajax表单声明GET时发布了一篇文章。当我把它们都放到POST或GET上时,我确实得到了正确的请求,但事实是,我的请求不是Ajax请求。所以我想我总是被重定向,因为UpdateTargetID没有被使用
以下是我的页面上包含的脚本:
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
请问有什么问题 您已经在ajax选项中指定了get,然后将您的操作方法修饰为post
要么更改“获取帖子”,要么删除帖子,看看是否有帮助。这里似乎有几个问题 加载脚本 首先确保加载了正确的脚本以使用AjaxHelper 及 为了完整性,你应该
[HttpPost]
public PartialViewResult UploadImage(HttpPostedFileBase file)
{
return PartialView("_ProfileImagesPartial");
}
<div id="profpics">
@using (Ajax.BeginForm(actionName: "UploadImage", controllerName: "ProfileImages",
routeValues: new {},
ajaxOptions: new AjaxOptions { UpdateTargetId = "profpics", HttpMethod = "POST" },
htmlAttributes: new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<input type="file" name="file" id="imgInp" />
<input type="submit" value="OK" />
}
</div>
[HttpPost]
public PartialViewResult上载映像(HttpPostedFileBase文件)
{
返回PartialView(“_profileimagespatial”);
}
@使用(Ajax.BeginForm)(actionName:“UploadImage”,controllerName:“ProfileImages”,
RouteValue:新{},
ajaxOptions:new ajaxOptions{UpdateTargetId=“profpics”,HttpMethod=“POST”},
HtmlatAttributes:new{enctype=“multipart/form data”})
{
@Html.AntiForgeryToken()
}
我拿出了一些东西,因为我们想确保基本的Ajax.BeginForm()
示例在没有额外运动部件的情况下工作。如果可行,则继续操作并恢复额外的零件。如果表单此时正在重定向,则可能会出现某种错误——通常是由于脚本错误
AJAX文件上传
您试图通过AJAX上传文件,需要注意一些问题。与其重复非常好的答案,不如从这里开始:
我注意到您正在使用AjaxForm的UpdateTargetId属性。我删除了我的答案,因为它与您试图实现的目标适得其反。我发现一本手册的内容与您的内容完全相同。所以我不认为这真的会适得其反。当我在ajax中声明GET时,我的电话一直在发帖子,我在这里读到了另一个问题,这是因为我没有使用不引人注目的脚本,但我确实使用了它们:sStrange。我通过nu-get软件包添加了ajax,但我似乎有一个过时的版本。谢谢你的帮助。
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/jquery-1.11.0.js"></script>
<script src="~/jquery.unobtrusive-ajax.js"></script>
[HttpPost]
public PartialViewResult UploadImage(HttpPostedFileBase file)
new AjaxOptions { HttpMethod = "POST", ... }
[HttpPost]
public PartialViewResult UploadImage(HttpPostedFileBase file)
{
return PartialView("_ProfileImagesPartial");
}
<div id="profpics">
@using (Ajax.BeginForm(actionName: "UploadImage", controllerName: "ProfileImages",
routeValues: new {},
ajaxOptions: new AjaxOptions { UpdateTargetId = "profpics", HttpMethod = "POST" },
htmlAttributes: new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<input type="file" name="file" id="imgInp" />
<input type="submit" value="OK" />
}
</div>