Ich muss versteckte ID-Wert an den Controller übergeben. Also habe ich es auf die folgende Weise versucht, aber ich bekomme Werte.Wie verberge ich ID-Wert an den Controller in asp.net mvc 3
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>
Wie @Html.HiddenFor(model => Model.DomainViews[i].DomainID, new { id = "hdnDomainID" })
Wert an die Steuerung weitergeben müssen:
public ActionResult RoleCreate()
{
userType type = new userType();
List<DomainView> EmpList = type.GetAllRoleModulesViews();
Role objBind = new Role();
objBind.DomainViews = EmpList;
return View(objBind);
}
In obigen Code, wie ich DomainID Wert erhalten ..
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;
}
}
Module:
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; }
}
Wie ID-Wert an den Controller übergeben? –
Die Verwendung des 'HiddenFor'-Elements ist der richtige Ansatz, Sie übergeben das gesamte Modell an den Controller (Post-Methode), wo alles korrekt gebunden ist auch ausgefüllt – thmshd