Hello I'm a total noob at Mvc and I can't seem to figure this one out myself.
I have some Html:
@using (Html.BeginForm())
{
foreach (Contribution ctrib in Model)
{
<p>@ctrib.Message.Content @ctrib.Likes @ctrib.Reports</p>
<a href="#" onclick="@{((TimelinePageController)this.ViewContext.Controller).Like(Convert.ToInt32(ctrib.ID));};window.location.reload()">Like</a>
<a href="#" onclick="@{((TimelinePageController)this.ViewContext.Controller).Report(Convert.ToInt32(ctrib.ID));};window.location.reload()">Report</a>
}
}
I have a controller:
[HttpPost]
public void Like(int ID)
{
Contribution C = new Contribution(ID);
C.likePost();
}
[HttpPost]
public void Report(int ID)
{
Contribution C = new Contribution(ID);
C.ReportPost();
}
I clearly see "Onclick" as function on the <a>
but it activates on page load.
Also whenever I press one of the 2 <a>
s both of them trigger the amount of times there are contributions in the Model. I think that has to do with the foreach but that is my only way of accumulating contributions.
If anyone could point out my mistake or help me that would be lovely.
答案 0 :(得分:0)
我会让控制器通过重定向到您所在页面的主要操作来处理页面的重新加载。这是您的Like操作的一个小样本。报告应该非常相似。
观点:
<a href="/YourController/Like/@ctrib.ID">Like</a>
控制器:
[HttpPost]
public void Like(int ID)
{
Contribution C = new Contribution(ID);
C.likePost();
RedirectToAction("ActionName")
}
希望有所帮助!