Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC4是否可以向模型中的控制器返回两个复杂类型列表_C#_Asp.net Mvc 4_Razor_Model - Fatal编程技术网

C# ASP.NET MVC4是否可以向模型中的控制器返回两个复杂类型列表

C# ASP.NET MVC4是否可以向模型中的控制器返回两个复杂类型列表,c#,asp.net-mvc-4,razor,model,C#,Asp.net Mvc 4,Razor,Model,我有一个模型,其中包括两个类列表: public class JobStructureViewModel { public List<SimpleControl> Controls { get; set; } public List<JobStructureTasksModel> Tasks { get; set; } } 我的问题是,当表单发回控制器时,其中一个列表总是空的。如果删除其中一个列表,那么另一个列表将成功返回,但是框架似乎无法在单个视图

我有一个模型,其中包括两个类列表:

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

    public List<JobStructureTasksModel> Tasks { get; set; }
}
我的问题是,当表单发回控制器时,其中一个列表总是空的。如果删除其中一个列表,那么另一个列表将成功返回,但是框架似乎无法在单个视图中处理多个列表。 有没有办法做到这一点,或者我必须尝试另一种方法

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

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>
public类作业结构任务模型
{
公共字符串代码{get;set;}
公共字符串说明{get;set;}
公共字符串协定{get;set;}
public int TaskID{get;set;}
[必需(ErrorMessage=“输入所需数量”)]
[范围(1100,ErrorMessage=“数量必须介于1和100之间”)]
公共浮点请求数量{get;set;}
公共字符串skillset{get;set;}
公共布尔删除{get;set;}
}
@模型ConnectManager2.Models.JobStructureTasksModel
@模型代码
@Html.HiddenFor(x=>x.Code)
@型号.说明
@Html.HiddenFor(x=>x.Description)
@示范合同
@Html.HiddenFor(x=>x.Contract)
@TextBoxFor(x=>x.ReqQty,新的{style=“width:25%”,onchange=“taskqtychanged()”})
@Html.ValidationMessageFor(x=>x.ReqQty)
@CheckBoxFor(x=>x.Delete,new{taskid=@Model.taskid,onclick=“taskDeleted(this)”})
@Html.HiddenFor(x=>x.TaskID)
公共类SimpleControl
{
公共SimpleControl()
{
选项=新列表();
}
公共字符串模板{get;set;}
公共字符串CtrlType{get;set;}
公共字符串名称{get;set;}
公共字符串LabelText{get;set;}
公共字符串PageName{get;set;}
公共bool强制{get;set;}
公共字符串初始值{get;set;}
公共字符串值{get;set;}
public bool CheckBoxValue{get;set;}
[数据类型(DataType.Date)]
公共日期时间?日期值{get;set;}
公共TimeSpan?TimeVal{get;set;}
公共列表选项{get;set;}
公共重写字符串ToString()
{
返回名称+“;“+Value==null?”:值;
}
}
@模型JobClassLib.TemplateRoutines.SimpleControl
@Model.LabelText
@如果(Model.CtrlType==“textbox”)
{
@TextBoxFor(x=>x.Value,新的{@class=“表单控件添加作业字段”})
}
else if(Model.CtrlType==“multilitextbox”)
{
@TextAreaFor(x=>x.Value,新的{@class=“form control add job field”})
}
else if(Model.CtrlType==“combobox”| | Model.CtrlType==“outcombobox”)
{
@Html.DropDownListFor(m=>m.Value,
新建SelectList(Model.Options,Model.Value),新建{@class=“form control add job field”})
}
else if(Model.CtrlType==“复选框”)
{
@Html.CheckBoxFor(x=>x.CheckBoxValue)
}
else if(Model.CtrlType==“日期选择器”)
{
DateTime dt=(Model.DateVal.HasValue)?Model.DateVal.Value:DateTime.Now;
string dateString=dt.ToString(telectera.Common.BaseFormats.DateDBFormat);
@TextBox(“DateVal”,dateString,new{@type=“date”,@class=“form control”})
setInitialValue=函数(){
$(“#“+@Model.Name+”>input”).val(“@dateString”);
}
setInitialValue();
}
else if(Model.CtrlType==“时间选择器”)
{
@Html.TextBoxFor(model=>model.TimeVal,新的{ctrltype=“time”})
}
else if(Model.CtrlType==“listview”)
{
}
否则如果(Model.CtrlType==“客户”)
{
@Html.HiddenFor(x=>x.Value)
$('#'+'@Model.Name').hide();
}
@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)

您可以发回任意数量的列表。您的POST方法的签名是什么?您显示的代码可以绑定。那些
onclick
onchange
脚本都在做什么?它们调用例程来编辑模式对话框中的一些数据。经过进一步挖掘,我发现只有当控件列表中只有一个项时才会出现此问题。如果它有两个或更多项,则任务会正确返回到服务器,但由于某种原因,只有一件物品时,情况就不一样了!
[HttpPost]
public ActionResult CreateJob(JobStructureViewModel model)
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>