Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created on MVC application. In that I have added 3 anchor tags.On click of each anchor tag I want to call function from control within same view only. How can I do this?Please find below code
Code for Index.cshtml file
@{
string actionname = (here I want to pass action values i.e which anchor tag is clicked on basis of this pass action name i.e Create/Edit/Delete);
}
@using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { id = "submitForm" }))
{
New

Edit

Delete

}

In Controller class I have written following code
[HttpPost]
public ActionResult Create()
{
return RedirectToAction("Index");
}

[HttpPost]
public ActionResult Edit()
{
return RedirectToAction("Index");
}

[HttpPost]
public ActionResult Delete()
{
return RedirectToAction("Index");
}
Now how can I get which anchor tag is clicked. So according to that I will dynamically pass action name in Html.Beginform(. Please help me in this.
Posted

One of way by which u can achieve this one is to make separate Html.BeginForm
for each hyperlink.

i.e.

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "submitForm" }))
{
New
}

@using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { id = "submitForm" }))
{
Edit
}

@using (Html.BeginForm("Delete", "Home", FormMethod.Post, new { id = "submitForm" }))
{
Delete
}
 
Share this answer
 
Comments
PrachiMhatre 26-Sep-14 2:10am    
I want to keep only one Html.Beginform . And on click of anchor tag change value of action i.e. create, edit and delete
Murali Gowda 26-Sep-14 5:38am    
Hey PrachiMhatre, referring to your code, write a onclick event to each anchor tag. In onclick event prevent default behaviour and then using form id set the action.
@using (Html.BeginForm())
{
<!-- Write all your code here...-->
<!--You can use Anchor tag to redirect to different Actions of the same or different controller.-->


<<anchor onclick="return false" href="@HttpUtility.UrlDecode(Url.Action("ActionName", "ControllerName"))" >

<<anchor onclick="return false" href="@HttpUtility.UrlDecode(Url.Action("Another_ActionName", "Another_ControllerName", new { id = item.ID}))" >

}
 
Share this answer
 
v9

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900