Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 4 checkboxes in my view which are generated dynamically as for the values present in the database.So while clicking on submit button i want to save the values of the checkboxes which are all checked.PLease let me know solution for this
Posted

<input type="checkbox" name="selectedRoles" id="chkRoles" value="@role.RoleID">
@foreach (var userRole in userRoles)
{
if (userRole.UserID == curID && role.RoleID == userRole.RoleID)
{

isChecked2=true;
@(Html.Raw(isChecked2 ? "checked=\"checked\"" : ""))
}
}
/>

In this if you run the code at runtime name field will get your selected roles
then write your controller as something like this

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserID,Name,Email")] User user, string[] selectedRoles,string userRoleID)
{


int s=Convert.ToInt32(user.UserID);

if (ModelState.IsValid)
{

foreach (var item in selectedRoles)
{
userRole.RoleID = Convert.ToInt32(item);

db.UserRole.Add(userRole);
db.SaveChanges();
}
}


ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName", user.RoleID);
return View(user);
}</input>


or try dis link

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application[^]
 
Share this answer
 
v2
 
Share this answer
 
v2
Here's a simple example with explaination which might help you
click here
 
Share this answer
 
How to get the subgrid values to controller ...! when button click.
i want know the values when checkbox click



@using (Html.BeginForm("View1", "Login", FormMethod.Post))
{

@grid.GetHtml(
htmlAttributes: new { id = "gridT", width = "1200px" },
columns: grid.Columns(
grid.Column("systables.tabname", "Table Name"),
grid.Column("systables.object_id", " Object Id"),
//grid.Column("systables.object_id", "Check", a => MvcHtmlString.Create(String.Format("<input type='checkbox' value='systables.object_id' />"))),
@*grid.Column(format: @<text><input type="checkbox" name="ids" value="@item.systables.object_id" />, header: "Select"),*@
grid.Column(format: (item) =>
{
WebGrid subGrid = new WebGrid(source: item.syscolumns);
return subGrid.GetHtml(
htmlAttributes: new { id = "subT" },
columns: subGrid.Columns(
subGrid.Column("tabname", "Table Name"),
subGrid.Column("object_id", " Object Id"),
subGrid.Column("column_id", "Column Id"),
subGrid.Column("colname", "Column Name"),
subGrid.Column("coltype", "column Type")
//,subGrid.Column("colselect", "col select")
,subGrid.Column(format: @<text><input type="checkbox" name="ids" value="column_id" />, header: "Select")
//subGrid.Column("object_id", "Check", a => MvcHtmlString.Create(String.Format("<input type='checkbox' value='21575115' />")))
@*subGrid.Column(format: @<text><input type="checkbox" value="@syscolumns.object_id" />, header: "Select")*@
)
);
})
)
)
<input type="submit" value="Click" class="btn btn-default btn-primary" />


}
 
Share this answer
 
Comments
CHill60 29-Jul-15 9:50am    
If you have a question of your own then use the "Ask a Question" link - don't post questions as solutions to old posts. Take care when pasting your code in and choose the correct code option to ensure it is readable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900