这是真的吗? 我想要这样的东西工作,这是HTML代码的一部分:
@if (Request.IsAuthenticated)
{
<input type="button" value="1" onclick="location.href='@Url.Action("PayMonthMore", "Home")'" />
}
这是我调用的HomeController方法:
public ActionResult PayMonthMore()
{
DBModelContainer db = new DBModelContainer();
var user = db.user.First(u => u.username == HttpContext.User.Identity.Name);
var paid = user.paidToDate;
if (paid == null)
{
paid = DateTime.Now;
}
user.paidToDate = paid.Value.AddMonths(1);
db.SaveChanges();
return View();
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
答案 0 :(得分:0)
为什么不把它放在表格中然后删除onclick?
@if (Request.IsAuthenticated)
{
<form action="/Home/PayMonthMore" method="POST">
<input type="submit" value="1" />
</form>
}