使用我的视图中的HTMl.DropDownList将其他字段传递给操作方法

时间:2013-07-18 22:01:19

标签: c# asp.net-mvc

我有以下模型类: -

public partial class SecurityroleTypePermision
    {
        public int SecurityRoleID { get; set; }
        public int PermisionLevelID { get; set; }
        public int AssetTypeID { get; set; }

        public virtual PermisionLevel PermisionLevel { get; set; }
        public virtual SecurityRole SecurityRole { get; set; }
        public virtual TechnologyType TechnologyType { get; set; }
    }

以下视图: -

@using (Html.BeginForm("AssignPermisionLevel2", "SecurityRole"))
{@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<tr>
<th></th><th></th></tr>   
@{int i = 0;
foreach (var item in Model.TechnologyType.OrderBy(a=>a.Name)) {
string s = "Model." + item.Name.ToLower() + "PermisionLevel"    ;              
<tr><td class="f">
@item.Name : @Html.DropDownList("@[i]+PermisionLevelID", new SelectList(Model.PermisionLevel, "PermisionLevelID", "Name",s))
@Html.Hidden("@[i]+ AssetTypeID",item.AssetTypeID)
 @Html.Hidden("@[i]SecurityRoleID",Model.SecurityRole.SecurityRoleID)
@(i = i+1);
</td><td class="f"></td></tr>}
}
</tbody></table>
<input type="submit" value="Assign" class="btn btn-primary"/>
</div></div></div>}

以下Action方法: -

[HttpPost]
public ActionResult AssignPermisionLevel2(ICollection<SecurityroleTypePermision> list)
        {foreach (var c in list)
            {repository.InsertOrUpdateSecurityroleTypePermisions(c); }
            repository.Save();
            return RedirectToAction("Index");}

但是我如何向我的动作方法提交包含下拉选择和两个隐藏字段的值列表? //提示我刚刚在视图中添加了[i],仅用于测试目的。

1 个答案:

答案 0 :(得分:0)

您可以通过将FormCollection传递给您的操作来获取所有这些值。

[HttpPost] 
public ActionResult AssignPermisionLevel2(FormCollection collection)
{
     // Get Post Params Here
     var list = collection["PermisionLevelID"];
     var assetTypeId = collection["AssetTypeID"];
     var securityRoldId = collection["SecurityRoleID"];
}