如何在asp.net mvc 3中将隐藏的Id值传递给控制器

时间:2017-12-19 06:26:42

标签: asp.net-mvc asp.net-mvc-3

我必须将隐藏的Id值传递给控制器​​。所以我尝试了以下方式,但我得到了价值。

cs.Html:

<ul id="tree" class="dd-list">
    @for (int i = 0; i < Model.DomainViews.Count(); i++)
    {
        <li class="dd-item">
            <a href="#">
                @Html.CheckBoxFor(model => model.DomainViews[i].IsChecked, new { @id = @Model.DomainViews[i].DomainID })
                <label for="@Model.DomainViews[i].DomainID">@Model.DomainViews[i].DomainName</label>
                @Html.HiddenFor(model => Model.DomainViews[i].DomainID, new { id = "hdnDomainID" })
            </a>
            <ul class="dd-list">
                <li class="dd-item">
                    <a href="#">
                        <input type="checkbox" id="2">
                        <label for="2"> Level 2 - 1</label>
                    </a>
                    <ul class="dd-list">
                        <li class="dd-item">
                            <a href="#">
                                <input type="checkbox" id="3">
                                <label for="3"> Level 3 - 1</label>
                            </a>
                        </li>
                        <li class="dd-item">
                            <a href="#">
                                <input type="checkbox" id="4">
                                <label for="4"> Level 3 - 2</label>
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    }
</ul>

如何将@Html.HiddenFor(model => Model.DomainViews[i].DomainID, new { id = "hdnDomainID" })值传递给控制器​​:

public ActionResult RoleCreate()
{
    userType type = new userType();
    List<DomainView> EmpList = type.GetAllRoleModulesViews();
    Role objBind = new Role();
    objBind.DomainViews = EmpList;
    return View(objBind);
}

在上面的代码中我如何获得DomainId Value ..

public List<DomainView> GetAllRoleModulesViews()
{
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admin"].ConnectionString))
    {
        List<DomainView> EmpList = new List<DomainView>();
        SqlCommand com = new SqlCommand("MEDEIL_DomainMaster_SelectAll", conn);
        com.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt = new DataTable();

        conn.Open();
        da.Fill(dt);
        conn.Close();
        foreach (DataRow dr in dt.Rows)
        {
            EmpList.Add(new DomainView
            {
                DomainID = Convert.ToInt32(dr["DomainID"]),
                DomainCode = Convert.ToString(dr["DomainCode"]),
                DomainName = Convert.ToString(dr["DomainName"]),
                CreatedBy = Convert.ToInt32(dr["CreatedBy"] == DBNull.Value ? null : dr["CreatedBy"].ToString()),
                CreatedDate = Convert.ToDateTime(dr["CreatedDate"]),
                ModifiedBy = Convert.ToInt32(dr["ModifiedBy"] == DBNull.Value ? null : dr["ModifiedBy"].ToString()),
                ModifiedDate = Convert.ToDateTime(dr["ModifiedDate"] == DBNull.Value ? null : dr["ModifiedDate"].ToString())
            });
        }
        return EmpList;
    }
}

模块:

public class DomainView
{
    [Key]
    public int DomainID { get; set; }
    public string DomainCode { get; set; }
    public string DomainName { get; set; }
    public int TabOrder { get; set; }
    public string UserName { get; set; }
    public int CreatedBy { get; set; }
    public DateTime CreatedDate = DateTime.UtcNow;
    public int ModifiedBy { get; set; }
    public DateTime ModifiedDate = DateTime.UtcNow;
    public bool IsChecked { get; set; }
    public IEnumerable<DomainView> DomainViews { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我更喜欢的最简单的方法是使用名为编辑器模板的MVC功能。您需要在正确的视图文件夹(或共享文件夹)中创建子目录EditorTemplates,然后在那里创建DomainView.cshtml。该文件包含的标记与for - 循环的正文非常相似,但DomainViews[i]被删除,因为模板仅适用于单个项目:

@model DomainView // <-- you'll need your Namespace here of course

<li class="dd-item">
    <a href="#">
    @Html.CheckBoxFor(model => model.IsChecked, new { @id = @Model.DomainID })
    <label for="@Model.DomainID">@Model.DomainName</label>
    @Html.HiddenFor(model => Model.DomainID, new { id = "hdnDomainID" })
    </a>
    ......
</li>

在您的主页面中放置元素<ul id="tree" class="dd-list">,您的代码将是:

<ul id="tree" class="dd-list">
    @Html.EditorFor(model => model.DomainViews)
</ul>

MVC编辑器模板引擎将为您生成正确的名称,当回发时,模型绑定器将确保正确填充数组。