无法检索从db-POST值生成的动态控件(模型绑定)

时间:2015-06-17 03:59:58

标签: asp.net-mvc model controller viewmodel modelbinders

我一直在开发一个ASP.NET MVC页面,我需要从数据库中引入控件,因此在数据库中我创建了控件详细信息,如name,value,id。

单向GET路径完成,现在在POST中我发现我无法在模型绑定器的帮助下获得IEnumerable<MyControls>,希望这对我来说很好学习如果有人告诉我什么我错过了一些错误或一些建筑上的东西请帮我完成这项工作。

模型

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; }
}

视图模型

public class ControlViewModel
{
    public IEnumerable<QuestionaireControlMasterModel> ListofControls { get; set; }
}

数据库

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),
)

控制器

     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 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>

    }

0 个答案:

没有答案