在asp.net mvc中传递复杂对象

时间:2014-03-22 17:45:43

标签: asp.net asp.net-mvc-4

我正在尝试将复杂对象从我的视图传递给我的控制器。

我赋予客户端创建无限列表框的能力。

如何将其传输到控制器?

以下是说明: 对于每个CompanyFeedbackTopic,客户端都可以插入无限CompanyFeedBackQuestion(Text with int)

我有这个对象:

public class CompanyFeedbackTopic
    {
        /***fields***/
        private readonly int m_topicId;
        private int m_companyFeedbackId;   
        private int m_formatFeedbackId;    
        private int m_percent;
        private List<CompanyFeedbackQuestion> m_companyFeedbackQuestionList;

        /***constractors***/
        public CompanyFeedbackTopic(int topicId, int companyFeedbackId, int formatFeedbackId, int percent, 
                                    List<CompanyFeedbackQuestion> companyFeedbackQuestionList)
        {
            m_topicId = topicId;
            m_companyFeedbackId = companyFeedbackId;
            m_formatFeedbackId = formatFeedbackId;
            m_percent = percent;
            m_companyFeedbackQuestionList = companyFeedbackQuestionList;
        }

        /***get/set***/
        public int TopicId
        { get { return m_topicId; } }
        public int CompanyFeedbackId
        { get { return m_companyFeedbackId; } set { m_companyFeedbackId = value; }}
        public int FormatFeedbackId
        { get { return m_formatFeedbackId; } set { m_formatFeedbackId = value; }}
        public int Percent
        { get { return m_percent; }  set { m_percent = value; }}
        public List<CompanyFeedbackQuestion> CompanyFeedbackQuestionList
        { get { return m_companyFeedbackQuestionList; } set { m_companyFeedbackQuestionList = value; }
        }

        /***functions***/
    }


public class CompanyFeedbackQuestion
    {
        /***fields***/
        private readonly int m_questionId;
        private int m_topicId;
        private string m_question;
        private int m_fieldGradeOptions;

        /***constractors***/
        public CompanyFeedbackQuestion(int questionId, int topicId, string question, int fieldGradeOptions)
        {
            m_questionId = questionId;
            m_topicId = topicId;
            m_question = question;
            m_fieldGradeOptions = fieldGradeOptions;            
        }

        /***get/set***/
        public int QuestionId
        { get { return m_questionId; } }
        public int TopicId
        { get { return m_topicId; } set { m_topicId = value; }}
        public string Question
        {  get { return m_question; } set { m_question = value; } }
        public int FieldGradeOptions
        { get { return m_fieldGradeOptions; } set { m_fieldGradeOptions = value; }}   
    }

这就是观点:

@using (Html.BeginForm("SubmitNewForm", "ManageFeedback", FormMethod.Post))
{
    @Html.Label("Choose feedback type: ")
    @Html.DropDownListFor(model => model.SelectedFeedbaclType, Model.FeedbackTypesDic.Select(i => new SelectListItem() { Text = i.Value, Value = i.Key.ToString() }), "---")

    <div id="formTable">
        @foreach (var item in Model.FormatFeedbackDic)
        {
            <h4>@item.Value</h4>
            <table id="@item.Key">
                <tr>
                    <td class="tableHeader">#</td>
                    <td class="tableHeader">question</td>
                    <td class="tableHeader">Grade Options</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>@Html.TextBox("SS", "", new { style = "width:500px" })</td>
                    <td>@Html.DropDownList("fieldGradeOptions", Model.GetDropDownList()) </td>
                </tr>
            </table>
            <br />
            <button onclick="AddNewRow(@item.Key);">+</button>
            <hr />
        }
    </div>
    <br />
    <button onclick="SubmitForm();">Create new feedback</button>
}

如何将其传输到视图?

1 个答案:

答案 0 :(得分:1)

你必须做两件事

  1. 用简单的for循环替换所有foreach循环。示例 -

    for(int i = o; i&lt; Model.FormatFeedbackDic.Count; i ++){}

  2. 2.更换此行//按钮onclick =“SubmitForm();”&gt;使用

    创建新的反馈

    / input type =“sumbit”/&gt;

    现在它将复杂模型传递回控制器