在Html.Beginform中动态传递操作名称 [英] Dynamically pass action name in Html.Beginform
问题描述
我在MVC应用程序上创建了。在那里我添加了3个锚标签。点击每个锚标签我只想在同一视图中从控件调用函数。我怎么能这样做?请找到下面的代码
Index.cshtml文件的代码
@ {
string actionname =(这里我想要传递动作值,即根据此传递动作名称单击哪个锚标记,即创建/编辑/删除);
}
@using(Html.BeginForm(编辑) ,Home,FormMethod.Post,new {id =submitForm}))
{
新增
编辑
删除
}
在Controller类中,我写了以下代码
[HttpPost]
public ActionResult Create()
{
返回RedirectToAction(Index);
}
[HttpPost]
public ActionResult Edit()
{
返回RedirectToAction(Index); < br $>
}
[HttpPost]
public ActionResult删除()
{
返回RedirectToAction(Index);
}
现在我该如何获得点击的锚标记。因此,根据我将动态传递Html.Beginform中的动作名称(。请帮助我。
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.
推荐答案
你可以实现这一目的的方法之一是为每个超链接单独制作Html.BeginForm
。
ie
@using(Html.BeginForm(Create,Home,FormMethod.Post,new {id =submitForm}))
{
New
}
@using(Html.BeginForm(编辑,主页,FormMethod.Post,新{id =submitForm} ))
{
编辑
}
@using(Html。 BeginForm(删除,主页,FormMethod.Post,新{id =submitForm}))
{
删除
}
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
}
@using(Html.BeginForm())
{
<! - 在这里写下你所有的代码.. .-->
<! - 您可以使用Anchor标记重定向到相同或不同控制器的不同操作.-->
<< anchor onclick =return falsehref =@ HttpUtility.UrlDecode(Url.Action(ActionName) ,ControllerName))>
<< anchor onclick =return falsehref =@ HttpUtility.UrlDecode(Url.Action(Another_ActionName ,Another_ControllerName,新{id = item.ID}))>
}
@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}))" >
}
这篇关于在Html.Beginform中动态传递操作名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!