ASP.NET MVC4是否可以将两个复杂类型列表返回到模型

时间:2017-03-16 08:45:32

标签: c# asp.net-mvc-4 razor model

我有一个模型,其中包含(除其他外)两个类列表:

public class JobStructureViewModel
{ 
    public List<SimpleControl> Controls { get; set; }

    public List<JobStructureTasksModel> Tasks { get; set; }
}

我使用编辑器模板在Razor视图中显示它们:

@model ConnectManager2.Models.JobStructureViewModel

   @using (Ajax.BeginForm("CreateJob", "JobCreator", new AjaxOptions { UpdateTargetId = "datagrid" }))
    {

    @Html.EditorFor(x => x.Tasks)

   @Html.EditorFor(x => x.Controls)
   <input style="display: none; width: 150px; height: 100%; margin: 0px; float: right; margin-right: 1.7em;" class="btn btn-default" id="createJobInput" type="submit" value="Submit Job" />
}

这是我的POST方法的签名:

[HttpPost]
public ActionResult CreateJob(JobStructureViewModel model)

我的问题是,当表单发回给控制器时,其中一个列表始终为null。如果我删除其中一个列表然后另一个列表成功返回,但框架似乎无法在单个视图中处理多个列表。 有没有办法使这成为可能,还是我必须尝试另一种方法?

“任务”和“控件”以及他们使用的视图都有类。

public class JobStructureTasksModel
{

    public string Code { get; set; }

    public string Description { get; set; }

    public string Contract { get; set; }

    public int TaskID { get; set; }

    [Required(ErrorMessage = "Enter the required quantity")]
    [Range(1, 100, ErrorMessage = "Quantity must be between 1 and 100")]
    public float ReqQty { get; set; }

    public string skillset { get; set; }

    public bool Delete { get; set; }
}


@model ConnectManager2.Models.JobStructureTasksModel
<tr id ="@Model.TaskID">
<td>@Model.Code
    @Html.HiddenFor(x => x.Code)
</td>
<td> @Model.Description
     @Html.HiddenFor(x => x.Description)
</td> 
<td> @Model.Contract
    @Html.HiddenFor(x => x.Contract)
</td>
<td> @Html.TextBoxFor(x => x.ReqQty, new {style = "width:25%" ,onchange = "taskqtychanged()"})
     @Html.ValidationMessageFor(x => x.ReqQty)
</td>
<td>
    @Html.CheckBoxFor(x => x.Delete, new {taskid = @Model.TaskID,  onclick = "taskDeleted(this)" })
</td>
@Html.HiddenFor(x => x.TaskID)
</tr>


 public class SimpleControl
 {
    public SimpleControl()
    {
        Options = new List<string>();

    }

    public string Template { get; set; }
    public string CtrlType { get; set; }

    public string Name { get; set; }

    public string LabelText { get; set; }

    public string PageName { get; set; }

    public bool Mandatory { get; set; }

    public string InitialValue { get; set; }

    public string Value { get; set; }

    public bool CheckBoxValue { get; set; }

    [DataType(DataType.Date)]
    public DateTime? DateVal { get; set; }

    public TimeSpan? TimeVal { get; set; }

    public List<string> Options { get; set; }

    public override string ToString()
    {
        return Name + "; " + Value == null ? "" : Value;
    }
 }


@model JobClassLib.TemplateRoutines.SimpleControl
<div>
<div class="row padtop" id ="@Model.Name">
    <div class="col-xs-3">
        <label>@Model.LabelText</label>
    </div>
    <div class="col-xs-6">
        @if (Model.CtrlType == "textbox")
        {
            <td>@Html.TextBoxFor(x => x.Value, new { @class = "form-control add-job-field" })
            </td>
        }
        else if (Model.CtrlType == "multilinetextbox")
        {
            <td>@Html.TextAreaFor(x => x.Value, new { @class = "form-control add-job-field" })
            </td>
         }
         else if (Model.CtrlType == "combobox" || Model.CtrlType == "outcomecombobox")
         {
            <td>
                @Html.DropDownListFor(m => m.Value,
new SelectList(Model.Options, Model.Value), new { @class = "form-control    add-job-field" })
            </td>
        }
        else if (Model.CtrlType == "checkbox")
        {
            <td>@Html.CheckBoxFor(x => x.CheckBoxValue)
            </td>
        }
        else if (Model.CtrlType == "datepicker")
        {
            DateTime dt = (Model.DateVal.HasValue) ? Model.DateVal.Value : DateTime.Now;
            string dateString = dt.ToString(Telecetera.Common.BaseFormats.DateDBFormat);
            <td>@Html.TextBox("DateVal", dateString, new { @type = "date", @class = "form-control" })
            </td>

            <script>
                setInitialValue = function () {
                    $("#" + @Model.Name + " > input").val('@dateString');
                }
                setInitialValue();
            </script>

        }
        else if (Model.CtrlType == "timepicker")
        {
<td>@Html.TextBoxFor(model => model.TimeVal, new { ctrltype = "time" })
            </td>
        }
        else if (Model.CtrlType == "listview")
        {
            <td>
                <input class="btn btn-default min-width-btn"  type="button" onclick="jobCreationLVClicked('@Model.Name','@Model.Template','@Model.LabelText')" lvid="@Model.Name" value="@Model.GetLVCount(Model.Name, Model.Template)" />
            </td>
        }
        else if (Model.CtrlType == "customer")
        {
            @Html.HiddenFor(x => x.Value)
            <script>
                $('#'+ '@Model.Name').hide();
            </script>
        }
    </div>
    @Html.HiddenFor(x => x.Name)
    @Html.HiddenFor(x => x.PageName)
    @Html.HiddenFor(x => x.LabelText)
    @Html.HiddenFor(x => x.CtrlType)
    @Html.HiddenFor(x => x.Template)
    @Html.HiddenFor(x => x.UserID)

</div>
</div>

0 个答案:

没有答案