我需要html按钮来调用没有js的控制器方法

时间:2015-03-16 06:17:50

标签: c# asp.net-mvc razor

这是真的吗? 我想要这样的东西工作,这是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 }               
    );
}

1 个答案:

答案 0 :(得分:0)

为什么不把它放在表格中然后删除onclick?

@if (Request.IsAuthenticated)
{
   <form action="/Home/PayMonthMore" method="POST">
        <input type="submit" value="1" />
   </form>
}