在部分视图中基于viewdata更改BeginForm操作?

时间:2013-03-17 14:13:10

标签: asp.net asp.net-mvc razor view partial-views

我有一个在同一页面上多次存在的部分视图。这将添加到视图中:

@Html.Partial("PersonalTagPartial", Model.PT, new ViewDataDictionary{{"TagTypeControl", 1}} )

@Html.Partial("PersonalTagPartial", Model.PT, new ViewDataDictionary{{"TagTypeControl", 0}} )

然后在部分视图中,我得到了一个这样的提交表单:

@using (Html.BeginForm("AddPersonalTag", "Post", FormMethod.Post, new { id = "frmAddPersonalTags" })){

当TagTypeControl设置为0时,如何将AddPersonalTag更改为RemovePersonalTag?

1 个答案:

答案 0 :(得分:0)

试试这样:

@using (Html.BeginForm((ViewBag.TagTypeControl == "1" ? "AddPersonalTag" : "RemovePersonalTag"), "Post", FormMethod.Post, new { id = "frmAddPersonalTags" }))
{
    ...
}

但你也可以直接将动作名称传递给partial:

@Html.Partial("PersonalTagPartial", Model.PT, new ViewDataDictionary { { "SubmitTo", "AddPersonalTag" } })
@Html.Partial("PersonalTagPartial", Model.PT, new ViewDataDictionary { { "SubmitTo", "RemovePersonalTag" } })

然后在部分内部:

@using (Html.BeginForm(ViewBag.SubmitTo, "Post", FormMethod.Post, new { id = "frmAddPersonalTags" }))
{
    ...
}