【发布时间】:2016-05-10 21:16:11
【问题描述】:
我有一个这样的view:
<div class="container content-sm">
@{
if (Request.IsAuthenticated)
{
<a>my profile</a>
}
else
{
if (Request.Url != null)
{
<a>Log in</a>
}
}
}
<div class="row">
<div class="col-md-9">
<div class="blog-item">
some html tags ...
</div>
<div>
<a>Next article</a>
</div>
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<div class="post-comment hidden-print" id="comment-tool-box">
@using (Ajax.BeginForm("SaveComment", "News", new { id = Model.Blog.Id }, new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "CommentToolPanel",
LoadingElementId = "loader",
OnBegin = "OnBegin",
OnSuccess = "OnComplete",
OnFailure = "OnFailure"
}, new {@class = "my-form"}))
{
@Html.AntiForgeryToken()
@Html.Partial("_PartialComment")
}
</div>
</div>
</div>
和controller:
[Route("en-us/blogs/explore/show/{year}/{month}/{day}/{id}/{title}")]
public ActionResult Details(string year, string month, string day, int id)
{
try
{
var model = _db.Table.ToList();
return View(model);
}
catch (Exception exception)
{
_logger.Log(exception);
return View("Index");
}
}
public string Captcha(int id)
{
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));
var obj = new RandomStringGenerator(true, true, true, false)
{
MinLowerCaseCharacters = 2,
MinNumericCharacters = 2,
MinUpperCaseCharacters = 2
};
var input = obj.Generate(7);
var bmp = new Bitmap(1, 1);
var graphics = Graphics.FromImage(bmp);
var font = new Font("Arial", 20f);
var stringSize = graphics.MeasureString(input, font);
bmp = new Bitmap
(
bmp,
(int)stringSize.Width,
(int)stringSize.Height
);
graphics = Graphics.FromImage(bmp);
graphics.DrawString(input, font, Brushes.LightGray, 0, 0);
font.Dispose();
graphics.Flush();
graphics.Dispose();
var width = bmp.Width;
var height = bmp.Height;
var tmp = new Random();
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
var r = tmp.Next(0, 255);
var g = tmp.Next(0, 255);
var b = tmp.Next(0, 255);
bmp.SetPixel(x, y, Color.FromArgb(255, r, g, b));
y++;
}
x++;
}
MemoryStream stream;
using (stream = new MemoryStream())
{
bmp.Save(stream, ImageFormat.Png);
}
var image = stream.ToArray();
var imageBase64Data = Convert.ToBase64String(image);
var imageDataUrl = $"data:image/png;base64,{imageBase64Data}";
var captchaImage =
$"<a data-ajax=\"true\" data-ajax-mode=\"replace\" data-ajax-update=\"#capt1\" href=\"/News/Captcha\"><img src='{imageDataUrl}' title=\"Click to reload a new image\" /></a>";
System.Web.HttpContext.Current.Session["maCaptcha"] = input;
Session["maCaptcha"] = input;
return captchaImage;
}
和_PartialComment查看
<div id="CommentToolPanel" class="hidden-print">
<header>Leave a Comment</header>
<fieldset>
<section>
@{
var opts1 = new AjaxOptions
{
UpdateTargetId = "capt1"
};
}
<span id="capt1">
@Html.Action("Captcha", "News")
</span>
@Html.TextBoxFor(m => m.CaptchaString, new
{
id = "captchaImage",
name = "captchaImage",
type = "text",
placeholder = "Captcha code is case censitive."
})
</section>
</fieldset>
<footer>
<button type="submit" class="button" id="btnComment">Submit</button>
</footer>
第一次加载视图时,一切都很好,但是,当我刷新浏览器时,控制器中的Details 方法不会触发,但是页面的内容会更新。
假设数据库中有 2 条记录。第一次显示记录#1(到目前为止还不错),点击下一步,记录#2也会显示(似乎很好),现在,我尝试再次显示记录#1,它会,但是Details方法没有被触发。
所以,我的问题是:如果用户登录并浏览该页面,然后注销然后返回该页面,我的个人资料链接仍然显示,因为,视图未更新。
我试试这些东西:
ASP.NET MVC AjaxForm not updating partial view properly
Update A Div And Partial View Using Ajax.BeginForm On Form Submit
[Walkthrough] Updating Partial Views with Unobtrusive AJAX in MVC 3
View not updating after post
但没有帮助:(
编辑:
当我从 _PartialComment 视图中删除这部分代码时,问题将得到解决:
@Html.Action("验证码", "新闻")
@Html.TextBoxFor(m => m.CaptchaString, 新
{
id = "验证码图像",
名称=“验证码图像”,
类型=“文本”,
placeholder = "验证码区分大小写。"
})
你能告诉我我的错误是什么,哪些部分是错误的?
我该如何解决这个问题?
【问题讨论】:
标签: jquery ajax asp.net-mvc