Mvc 4模型绑定无法动态添加局部视图

时间:2014-03-14 10:58:37

标签: asp.net-mvc-4 binding model partial-views

我正在尝试创建一个表单,用户可以在其中添加控件。我有主要观点

@model MVCDynamicFormGenerator.Models.FormViewModel
@{
    ViewBag.Title = "Create";
}


 @using (@Html.BeginForm())
{
    <fieldset>
        @Html.HiddenFor(form => form.Form.Uid)
        @Html.Hidden("ListFields", ViewData["ListFields"])
        <p>
            @Html.LabelFor(form => form.Form.FormName)
            @Html.TextBoxFor(form => form.Form.FormName)
        </p>
        <div id="FormFieldList">
            @foreach (var formfield in Model.FormFields)
            {
                switch (formfield.ControlType)
                {
                    case ("Textbox"):
                        Html.RenderPartial("Textbox", formfield);
                        break;

                }
            }
        </div>
        <h4>
            <a href="javascript:;" id="newFieldSelect" style="display: none;">[+] Add a Field</a>
        </h4>
        <div id="FieldType">
            <table>
                <tr>
                    <th>
                        Select a Field Type
                    </th>
                </tr>
                <tr>
                    <td>
                        @Html.DropDownList("FieldTypes", new SelectList(Model.FormFields[0].FormFieldTypes, "Value", "Text"), new { id = "SelectedFieldUid" })
                        @Html.ActionLink("Add Field", "NewFormField", new { formId = ViewContext.FormContext.FormId, selectedFieldType = "SelectedFieldUid" }, new { id = "newFormField" })
                        @Html.ValidationMessageFor(model => model.FormFields)
                    </td>
                </tr>
            </table>
        </div>
        <p>
            <input type="submit" value="Create" />
            <input type="button" value="Cancel" '@Url.Action("List")');" />
        </p>
    </fieldset>     
}

在下拉列表更改时,我正在加载一个正在运行的部分视图(用户可以添加n次)

 @model MVCDynamicFormGenerator.Models.FormFieldViewModel
<div class="FormField">
    @using (@Html.BeginForm())
    {
        <table>
            <tr>
                <th>
                    Form Field
                </th>
                <th>
                    Field Type
                </th>

            </tr>
            <tr>
                <td style="width: 45%;">
                    @Html.TextBoxFor(formfield => formfield.FormFieldName)
                    @Html.ValidationMessageFor(formfield => formfield.FormFieldName)
                </td>
                <td style="width: 25%;">
                    @Html.DropDownListFor(formfield => formfield.SelectedFormFieldType,
                                                        new SelectList(Model.FormFieldTypes, "Value", "Text",
                                                                                Model.SelectedFormFieldType),
                                                               new { disabled = "disabled" })
                    @Html.HiddenFor(formfield => formfield.SelectedFormFieldType)
                    @Html.ValidationMessageFor(formfield => formfield.SelectedFormFieldType)
                </td>
               </tr>

        </table>
    }
</div>



///  form models
public class FormViewModel
{
    //Properties
    public Form Form { get; set; }

    public List<FormFieldViewModel> FormFields { get; set; }

    //Constructor
    public FormViewModel()
    {
        Form = new Form();
        FormFields = new List<FormFieldViewModel>();
    }
}


  public class FormFieldViewModel
  {
      public string FormFieldName { get; set; }
   public string SelectedFormFieldType { get; set; }
  }

  controller methods 
    [HttpPost]
    public ActionResult Create(FormViewModel viewModel)
    {
        return View();
    }

所有与主视图相关的字段信息都可用,但FormFieldViewModel列表显示零计数

任何帮助或建议来解决此问题

0 个答案:

没有答案