MVC3:按钮同时发送的形式(模型)值和一个额外的参数(MVC3: button to send b

2019-09-17 15:25发布

在MVC3项目,我使用Html.BeginForm张贴一些(模型 - )值。 除了这些我想送一个额外的参数不是形式(模型),但在ViewBag的一部分。 现在,当我使用一个按钮(代码在这里回答: 创建HtmlButtonExtension MVC3剃刀错误 ),所有的表单值都张贴,但额外的参数保持为空。 当我使用ActionLink的,参数公布,但形式值不:)任何知道我可以将二者结合起来? 谢谢!

@Html.Button("Generate!", new { id = ViewBag.ProjectID })
@Html.ActionLink("Generate!", "Post", new { id = @ViewBag.ProjectID })

Answer 1:

我的建议是在你的App.Domain.Model像这样声明一个新对象

namespace App.Domain.Model
{
    public class CustomEntity
    {
        public Project projectEntity { get; set; }
        public int variableUsed { get; set; }
    }
}


在你看来,你可以通过使用CustomEntity.projectEntity和CustomEntity.variableUsed容易存取权限它们。

希望能帮助到你



Answer 2:

你可以这样做以下。

查看代码

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { @id = "frmId", @name = "frmId" }))
{
        @*You have to define input as a type button not as a sumit. you also need to define hidden variable for the extra value.*@
        <input type="hidden"  name="hndExtraParameter" id="hndExtraParameter" />
        <input value="Submit" type="button" id="btnSubmit" onclick="UpdateHiddenValue()" />
}

<script type="text/javascript">

function ValidateUser() {
$("#hndExtraParameter").val('Assignvaluehere');
$("#frmId").submit();
}

</script>

控制器代码

[HttpPost]
public ActionResult ActionName(Model model, string hndExtraParameter)
{
//Do your operation here.
}


文章来源: MVC3: button to send both form (model) values and an extra parameter