我有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,但是{{1} }因此,我的问题是:如果用户登录并浏览页面,然后退出并返回此页面,我的个人资料链接仍然是显示,因为视图没有更新
我尝试这些东西:
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(&#34; Captcha&#34;,&#34; News&#34;)
@ Html.TextBoxFor(m =&gt; m.CaptchaString,new
{
id =&#34; captchaImage&#34;,
name =&#34; captchaImage&#34;,
type =&#34; text&#34;,
占位符=&#34;验证码是案例敏感的。&#34;
})
你能否告诉我我的错误是什么,哪些部分是错的?
我该如何解决这个问题?
答案 0 :(得分:0)
通过在CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.Guid(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
方法中删除这些行,我的问题就解决了!
Captcha
我不知道为什么。