I have a view
like this:
<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>
and 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;
}
and _PartialComment
view
<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>
first time that the view is load, every things are fine, but, when I refresh the browser, Details
method in controller does not fire, however the content of page will renew.
Let's say there are 2 records in database. First time, the record #1 is displayed (so far so good), with click next, record #2 will displayed too (seems fine), now, I try to display record #1 again, it will, but Details
method does not fired.
So, my problem is: if user is logged in and browse the page and after that logged out and then return to this page, the my profile link is still displays, because, the view is not updated.
I try these things:
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
but not helped :(
Edit:
Problem will solved, when I removed this part of code from _PartialComment view:
@Html.Action("Captcha", "News")
@Html.TextBoxFor(m => m.CaptchaString, new
{
id = "captchaImage",
name = "captchaImage",
type = "text",
placeholder = "Captcha code is case censitive."
})
Would you please tell me what is(are) my mistake(s) nor which part(s) is(are) wrong?
How I can fix this problem?