Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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# 如何检查我的模型在BeginForm标记外是否为空_C#_Html_Razor - Fatal编程技术网

C# 如何检查我的模型在BeginForm标记外是否为空

C# 如何检查我的模型在BeginForm标记外是否为空,c#,html,razor,C#,Html,Razor,基本上我有一些隐藏字段,当返回的数据为空时会导致错误。我想做的是检查模型是否为空,如果为空,我想跳过整个Begin Form标记并移动到我的表上。但是,当我尝试此操作时,它仍然会停止并通过beginform标记。有没有办法改变这个 您可以在下面查看我的设置: @if (!Model.ReportData.Any()) { using (Html.BeginForm("ReportSelection", "Reports", FormMethod.Post)) {

基本上我有一些隐藏字段,当返回的数据为空时会导致错误。我想做的是检查模型是否为空,如果为空,我想跳过整个Begin Form标记并移动到我的表上。但是,当我尝试此操作时,它仍然会停止并通过beginform标记。有没有办法改变这个

您可以在下面查看我的设置:

@if (!Model.ReportData.Any()) 
{
    using (Html.BeginForm("ReportSelection", "Reports", FormMethod.Post))      
    {
        @Html.DropDownListFor(r => r.StatusCategoryID, new SelectList(Model.StatusCategories, "StatusCategoryID", "StatusCategoryDesc"), "Select Status", new { @class = "GRDropDown", @id = "ReportDD" })
        <input type="hidden" name="StatusReportID" value="1" />
        <input type="hidden" name="ClientID" value="@Model.ReportData.Select(r => r.ClientID).FirstOrDefault()" />
        if (Model.ReportData.Select(r => r.SupplierID).Any()) 
        { 
            <input type="hidden" name="SupplierID" value="@Model.ReportData.Select(r => r.SupplierID).FirstOrDefault()" />
        }
        else if (Model.ReportData.Select(r => r.ReviewPeriodID).Any()) 
        { 
            <input type="hidden" name="ReviewPeriodID" value="@Model.ReportData.Select(r => r.ReviewPeriodID).FirstOrDefault()" />
        }
        <button type="submit" value="Submit" class="btn btn-default">Submit</button>
    }
}
@if(!Model.ReportData.Any())
{
使用(Html.BeginForm(“ReportSelection”、“Reports”、FormMethod.Post))
{
@Html.DropDownListFor(r=>r.StatusCategoryID,新选择列表(Model.StatusCategories,“StatusCategoryID”,“StatusCategoryDesc”),“选择状态”,新{@class=“GRDropDown”,@id=“ReportDD”})
if(Model.ReportData.Select(r=>r.SupplierID).Any())
{ 
}
else if(Model.ReportData.Select(r=>r.ReviewPeriodID).Any())
{ 
}
提交
}
}

如果模型为空,如何阻止它执行beginform标记?我本以为我的IF会阻止它,但它没有。

你的情况是倒退的。你说“如果里面没有任何东西,请出示这张表格”:

您希望它是“如果里面有东西,请显示此表单”:


干杯,小伙子。不知道我怎么没抓到那个
@if (!Model.ReportData.Any()) {
@if (Model.ReportData.Any()) {