I have been developing an ASP.NET MVC page where I need to bring the controls from database, so in database I have created our control details like name, value, id.
The one way GET path is done, now in POST I find amazing that I am not able to get the IEnumerable<MyControls>
with the help of model binder, hope it would be nice learning for me If someone tell me whats wrong or some thing architecturally I am missing out pls help me with my piece of this work.
Model
public class QuestionaireControlMasterModel
{
public int Id { get; set; }
public int Questiono { get; set; }
public string Question { get; set; }
public string ControlType { get; set; }
public string ControlDescription { get; set; }
public string ControlName { get; set; }
// My question from Question table //
public string ControlLabel { get; set; }
public string ControlId { get; set; }
public string ControlValue { get; set; }
public string IsChecked { get; set; }
// for ddl list//
public List<string> ddlhtmllistppt { get; set; }
}
ViewModel
public class ControlViewModel
{
public IEnumerable<QuestionaireControlMasterModel> ListofControls { get; set; }
}
Database:
create table ControlMaster
(
ControlType int identity primary key,
ControlDescription nvarchar(500)
)
create table Questionaire
(
Questiono int identity(1,1) primary key,
Question nvarchar(500),
)
create table QuestionaireControlMaster
(
Id int identity (1,1) primary key,
Questiono int
Constraint fk_QuestionaireControlMaster_Questiono
references Questionaire(Questiono),
ControlType int
Constraint fk_QuestionaireControlMaster_ControlType
references ControlMaster(ControlType),
// Control Name I use in View //
ControlName1 nvarchar(500),
// Control Id I use in View //
ControlId1 nvarchar(500),
IsChecked1 nvarchar(500),
)
create table ValueMaster
(
ValueID int identity(1,1) primary key,
ControlValue nvarchar(500),
Id int
Constraint fk_ValueMaster_Id
references QuestionaireControlMaster(Id),
)
Controller
public ActionResult Index()
{
// View Model Object//
ControlViewModel controlviewmodel = new ControlViewModel();
using (var db = new OfficialEntities())
{
// Retrieval of data from QuestionMaster table
var retrievedlistfromdb = db.QuestionaireControlMasters.ToList();
//Creating a list of Questionaire table Model to store the list of controls needed//
List<QuestionaireControlMasterModel> listofquestionmodel = new List<QuestionaireControlMasterModel>();
// Storing the retrieved list of controls from db to our list in ViewModel //
// Mapping retrieved list from db to my list of controls //
foreach (var temp in retrievedlistfromdb)
{
QuestionaireControlMasterModel questionaireControlMasterModel = new QuestionaireControlMasterModel();
List<string> testinglist = new List<string>();
questionaireControlMasterModel.ControlType = temp.ControlMaster.ControlDescription;
if (questionaireControlMasterModel.ControlType == "checkbox")
{
questionaireControlMasterModel.ControlLabel = temp.Questionaire.Question;
questionaireControlMasterModel.ControlId = temp.ControlId1;
questionaireControlMasterModel.ControlName = temp.ControlName1;
using (var dbconnectovalues = new OfficialEntities())
{
var values = dbconnectovalues.ValueMasters.Where(x => x.Id == temp.Id).ToList();
if (values.Count == 1)
{
foreach (var item in values)
{
questionaireControlMasterModel.ControlValue = item.ControlValue;
}
}
}
questionaireControlMasterModel.IsChecked = temp.IsChecked1;
questionaireControlMasterModel.ddlhtmllistppt = testinglist;
listofquestionmodel.Add(questionaireControlMasterModel);
}
// For Dropdownlist and textbox separate mapping //
else
{
questionaireControlMasterModel.ControlLabel = temp.Questionaire.Question;
questionaireControlMasterModel.ControlId = temp.ControlId1;
questionaireControlMasterModel.ControlName = temp.ControlName1;
using (var dbconnectovalues = new OfficialEntities())
{
var values = dbconnectovalues.ValueMasters.Where(x => x.Id == temp.Id).ToList();
if (values.Count == 1)
{
foreach (var item in values)
{
questionaireControlMasterModel.ControlValue = item.ControlValue;
}
}
if (values.Count > 1)
{
foreach (var item in values)
{
string tempvar = item.ControlValue;
testinglist.Add(tempvar);
}
}
}
questionaireControlMasterModel.IsChecked = temp.IsChecked1;
questionaireControlMasterModel.ddlhtmllistppt = testinglist;
listofquestionmodel.Add(questionaireControlMasterModel);
}
// Attaching my List to the lIst property of the View Model//
controlviewmodel.ListofControls = listofquestionmodel;
}
//Passing ViewModel Object which contains the list of controls of Questionaire//
return View(controlviewmodel);
}
}
// Here is where the Model binder I am not able to understand why I am not able to get the list of QuestionaireMaster model which is a part of the ControlViewModel //
// Pls suggest If something is architecturally wrong or need to do a work around//
[HttpPost]
public ActionResult Index(ControlViewModel sampleobject)
{
// sample object is null, even I tried to get List<QuestionaireMasterModel> it was also null //
return View();
}
PartialView
// Bound to List of Controls //
@model IEnumerable<CDCPortal.Models.QuestionaireControlMasterModel>
<table>
@foreach (CDCPortal.Models.QuestionaireControlMasterModel DynamicModel in Model)
{
switch (DynamicModel.ControlType.ToLower())
{
case "textbox":
<tr>
<td>
@DynamicModel.ControlLabel
</td>
</tr>
<tr>
<td>
<input type="text" name="@DynamicModel.ControlName" id="@DynamicModel.ControlId" value="@DynamicModel.ControlValue" />
</td>
</tr>
break;
}
}
View
// View bound to Viewmodel //
@model CDCPortal.Models.ControlViewModel
@using (Html.BeginForm())
{
<div>
// Using the Partial View here to render up controls//
@Html.Partial("_ControlPartial", Model.ListofControls)
</div>
<div>
<input type="submit" name="SubmitButton" value="Submit" />
</div>
}