MVC4 Model in View has nested data - cannot get data in model
- by Taersious
I have a Model defined that gets me a View with a list of RadioButtons, per IEnumerable.
Within that Model, I want to display a list of checkboxes that will vary based on the item selected. Finally, there will be a Textarea in the same view once the user has selected from the available checkboxes, with some dynamic text there based on the CheckBoxes that are selected. What we should end up with is a Table-per-hierarchy.
The layout is such that the RadioButtonList is in the first table cell, the CheckBoxList is in the middle table cell, and the Textarea is ini the right table cell.
If anyone can guide me to what my model-view should be to achieve this result, I'll be most pleased...
Here are my codes:
//
// View Model for implementing radio button list
public class RadioButtonViewModel
{
// objects
public List<RadioButtonItem> RadioButtonList { get; set; }
public string SelectedRadioButton { get; set; }
}
//
// Object for handling each radio button
public class RadioButtonItem
{
// this object
public string Name { get; set; }
public bool Selected { get; set; }
public int ObjectId { get; set; }
// columns
public virtual IEnumerable<CheckBoxItem> CheckBoxItems { get; set; }
}
//
// Object for handling each checkbox
public class CheckBoxViewModel
{
public List<CheckBoxItem> CheckBoxList { get; set; }
}
//
// Object for handling each check box
public class CheckBoxItem
{
public string Name { get; set; }
public bool Selected { get; set; }
public int ObjectId { get; set; }
public virtual RadioButtonItem RadioButtonItem { get; set; }
}
and the view
@model IEnumerable<EF_Utility.Models.RadioButtonItem>
@{
ViewBag.Title = "Connect";
ViewBag.Selected = Request["name"] != null ? Request["name"].ToString() : "";
}
@using (Html.BeginForm("Objects" , "Home", FormMethod.Post) ){
@Html.ValidationSummary(true)
<table>
<tbody>
<tr>
<td style="border: 1px solid grey; vertical-align:top;">
<table>
<tbody>
<tr>
<th style="text-align:left; width: 50px;">Select</th>
<th style="text-align:left;">View or Table Name</th>
</tr>
@{
foreach (EF_Utility.Models.RadioButtonItem item in @Model )
{
<tr>
<td>
@Html.RadioButton("RadioButtonViewModel.SelectedRadioButton",
item.Name,
ViewBag.Selected == item.Name ? true : item.Selected,
new { @onclick = "this.form.action='/Home/Connect?name=" + item.Name + "'; this.form.submit(); " })
</td>
<td>
@Html.DisplayFor(i => item.Name)
</td>
</tr>
}
}
</tbody>
</table>
</td>
<td style="border: 1px solid grey; width: 220px; vertical-align:top; @(ViewBag.Selected == "" ? "display:none;" : "")">
<table>
<tbody>
<tr>
<th>Column
</th>
</tr>
<tr>
<td><!-- checkboxes will go here -->
</td>
</tr>
</tbody>
</table>
</td>
<td style="border: 1px solid grey; vertical-align:top; @(ViewBag.Selected == "" ? "display:none;" : "")">
<textarea name="output" id="output" rows="24" cols="48"></textarea>
</td>
</tr>
</tbody>
</table>
}
and the relevant controller
public ActionResult Connect()
{
/* TEST SESSION FIRST*/
if( Session["connstr"] == null)
return RedirectToAction("Index");
else
{
ViewBag.Message = "";
ViewBag.ConnectionString = Server.UrlDecode( Session["connstr"].ToString() );
ViewBag.Server = ParseConnectionString( ViewBag.ConnectionString, "Data Source" );
ViewBag.Database = ParseConnectionString( ViewBag.ConnectionString, "Initial Catalog" );
using( var db = new SysDbContext(ViewBag.ConnectionString))
{
var objects = db.Set<SqlObject>().ToArray();
var model = objects
.Select( o => new RadioButtonItem { Name = o.Name, Selected = false, ObjectId = o.Object_Id, CheckBoxItems = Enumerable.Empty<EF_Utility.Models.CheckBoxItem>() } )
.OrderBy( rb => rb.Name );
return View( model );
}
}
}
What I am missing it seems, is the code in my Connect() method that will bring the data context forward; at that point, it should be fairly straight-forward to set up the Html for the View.
EDIT
** So I am going to need to bind the RadioButtonItem to the view with something like the following, except my CheckBoxList will NOT be an empty set.
//
// POST: /Home/Connect/
[HttpPost]
public ActionResult Connect( RadioButtonItem rbl )
{
/* TEST SESSION FIRST*/
if ( Session["connstr"] == null )
return RedirectToAction( "Index" );
else
{
ViewBag.Message = "";
ViewBag.ConnectionString = Server.UrlDecode( Session["connstr"].ToString() );
ViewBag.Server = ParseConnectionString( ViewBag.ConnectionString, "Data Source" );
ViewBag.Database = ParseConnectionString( ViewBag.ConnectionString, "Initial Catalog" );
using ( var db = new SysDbContext( ViewBag.ConnectionString ) )
{
var objects = db.Set<SqlObject>().ToArray();
var model = objects
.Select( o => new RadioButtonItem { Name = o.Name, Selected = false, ObjectId = o.Object_Id, CheckBoxItems = Enumerable.Empty<EF_Utility.Models.CheckBoxItem>() } )
.OrderBy( rb => rb.Name );
return View( model );
}
}
}