检查 viewModel 是否等于 0 并显示
标签。

huangapple go评论66阅读模式
英文:

Create a logic to check if viewModel == 0 & show <div> tag

问题

I am trying to show a <div> when checking model is 0. However, after creating a logic to check whether model is 0, I am getting a white blank page. If the model is != 0, it is displaying the <div> with the model != 0.

我尝试在检查模型是否为0时显示一个<div>。然而,在创建了检查模型是否为0的逻辑之后,我得到了一个空白的白色页面。如果模型不等于0,它会显示带有模型不等于0的<div>。

I have put a breakpoint debugger to check whether the API is successfully called & everything works fine. The API is getting count = 0 if there are no premises in the DB & the count is != 0 if there are premises in the DB.

我已经设置了一个断点调试器来检查API是否成功调用,一切都正常。如果数据库中没有场所,API将获得count = 0,如果数据库中有场所,count就不等于0。

I have tried giving a div id to the <div> tag & style display = none so that it will be hidden by default & create a logic in script to check whether model is 0 then show the div with the id but it is still not working.

我尝试为<div>标签提供了一个div id,并设置style display = none,以便默认隐藏它,并在脚本中创建一个逻辑来检查模型是否为0,然后显示具有该id的div,但它仍然不起作用。

英文:

I am trying to show a <div> when checking model is 0. However, after creating a logic to check whether model is 0, i am getting white blank page. If the model is != 0 it is displaying the <div> with the model != 0

I have put a breakpoint debugger to check whether the the api is successfully called & everything works fine. The api is getting count = 0 if there is no premises in db & the count is != 0 if there is premises in db.

I have tried giving a div id to the <div> tag & style display = none so that it will be hidden by default & create a logic in script to check whether model is 0 then show the div with the id but it is still not working.

views.cshtml:

@model List&lt;MyTNB.MyHome.Web.Areas.Application.Models.MyHomeChecklist.HMOPremisesViewModel&gt;


&lt;div class=&quot;px-md-5 px-4 mt-4&quot;&gt;
    &lt;div class=&quot;card-title-blue-20-16 mb-2 font-weight-bold&quot;&gt;@Localizer.GetLocalizedString(&quot;HMO_ConnectToMyHome_MyPremises&quot;)&lt;/div&gt;
&lt;/div&gt;
@if (Model.Count == 0)
{
    &lt;!--#region No Premises Card--&gt;
    &lt;div class=&quot;container-fluid default-spacing-24 mt-md-3&quot;&gt;
        &lt;div class=&quot;block block--light br-xs-0&quot;&gt;
            &lt;div class=&quot;no-premise-icon&quot;&gt;
                &lt;img src=&quot;~/assets/img/hmo-connectmyhome-no-premise-icon.png&quot; /&gt;
            &lt;/div&gt;
            &lt;div class=&quot;no-premise-text card-title-inverse-lighter&quot;&gt;
            @Localizer.GetLocalizedString(&quot;HMO_ConnectToMyHome_NoPremise_Desc&quot;)
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;!--#endregion No Premises Card--&gt;
}
else
{
    &lt;div class=&quot;myhome-ul px-md-5 px-4 mb-2&quot;&gt;
        &lt;ul data-role=&quot;listview&quot; id=&quot;tasks&quot;&gt;
            &lt;!--#region My Premises Card--&gt;
            @foreach (var premise in Model)
            {
                &lt;li class=&quot;mb-2&quot;&gt;
                    &lt;div class=&quot;wrapper&quot; id=&quot;wrapper_@premise.PremisesID&quot;
                            onmousedown=&quot;_taskCardMouseDown(event, @premise.PremisesID)&quot;
                            onmouseup=&quot;_taskCardMouseUp(event, @premise.PremisesID)&quot;
                            ontouchstart=&quot;_taskCardTouchStart(event, @premise.PremisesID)&quot;
                            ontouchend=&quot;_taskCardTouchEnd(event, @premise.PremisesID)&quot;&gt;
                        &lt;div class=&quot;go&quot; style=&quot;width:0%;&quot;&gt;&lt;/div&gt;
                        &lt;div class=&quot;item&quot; id=&quot;item_@premise.PremisesID&quot;&gt;
                            &lt;div class=&quot;block block--light block--cardShadow flex justify-content-between custom-radius mb-md-4 mb-2&quot;&gt;
                                &lt;div class=&quot;flex center&quot;&gt;
                                    &lt;div class=&quot;mr-3&quot;&gt;
                                        &lt;img src=&quot;~/assets/img/hmo-connectmyhome-home-icon.svg&quot; /&gt;
                                    &lt;/div&gt;
                                    &lt;div&gt;
                                        &lt;div class=&quot;text-left task-description&quot; onclick=&quot;MyPremise(&#39;@premise.PremisesID.ToString()&#39;,&#39;@premise.ApplicationType&#39;)&quot;&gt;
                                            @premise.PremiseNickname
                                        &lt;/div&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;&lt;div class=&quot;block block--light block--cardShadow flex justify-content-between edit&quot; id=&quot;edit_@premise.PremisesID&quot; onclick=&quot;EditPremise(&#39;@premise.PremisesID.ToString()&#39;,&#39;@premise.ApplicationType&#39;)&quot;&gt;&lt;img src=&quot;~/assets/img/edit-white.svg&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;del block block--light block--cardShadow flex justify-content-between&quot; id=&quot;del_@premise.PremisesID&quot; onclick=&quot;openDelModal(&#39;@premise.PremisesID.ToString()&#39;)&quot;&gt;&lt;img class=&quot;&quot; src=&quot;~/assets/img/delete-white.svg&quot; /&gt;&lt;/div&gt;
                    &lt;/div&gt;
                &lt;/li&gt;                              
            }
            &lt;!--#endregion My Premises Card--&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
}

controller:

    [HttpGet(&quot;ConnectToMyHome&quot;)]
    public async Task&lt;ActionResult&gt; ConnectToMyHome()
    {
        try
        {
            var premises = await _hmoHelper.GetHMOPremisesAsync();

            return View(premises);
        }
        catch (Exception)
        {
            return RedirectToAction(&quot;Error&quot;, &quot;Home&quot;);
        }
    }

viewModel:

using System;

namespace MyTNB.MyHome.Web.Areas.Application.Models.MyHomeChecklist
{
    public class HMOPremisesViewModel
    {
        public Guid PremisesID { get; set; }
        public string PremiseNickname { get; set; }
        public string ApplicationType { get; set; }
        public string ApplicationID { get; set; }
        public int TaskType { get; set; }
    }
}

response:

    namespace MyTNB.MyHome.Web.Services.ApiModels.MyHomeChecklist
{
    public class GetHMOPremisesResponse : ApiResponseBody
    {
        public GetHMOPremisesViewModelContent Content { get; set; }
    }

    public class GetHMOPremisesViewModelContent
    {
        public List&lt;HMOPremisesViewModel&gt; Premises { get; set; }
    }

}

答案1

得分: 1

I am trying to show a when checking model is 0. However, after creating a logic to check whether model is 0, i am getting white blank page. If the model is != 0 it is displaying the with the model != 0

实际上,Model.Count 无法帮助您获得所需的结果,因为在视图中,我们无法计算整个模型,而只能计算特定索引元素或项目的数量。例如,您可以这样做:Model.FirstOrDefault().YourModelElements,因此如果列表为空,此块将不会执行。

然而,您可以使用 Model.Any() 来实现最简单和高效的方式,如果列表不为空,它将执行 if 块,否则将执行 else 块。

让我们看看实际操作中的示例:

控制器:

public IActionResult Index()
{
    var countCheckModel = new List<HMOPremisesViewModel>
    {

        new HMOPremisesViewModel(){ PremisesID = new Guid(), PremiseNickname = "Microsoft", ApplicationType = "A", ApplicationID ="101", TaskType =1},
        new HMOPremisesViewModel(){ PremisesID = new Guid(), PremiseNickname = "Amazon", ApplicationType = "B", ApplicationID ="201", TaskType =2},
        new HMOPremisesViewModel(){ PremisesID = new Guid(), PremiseNickname = "Google", ApplicationType = "C", ApplicationID ="303", TaskType =2},
    };

    var emptyList = new List<HMOPremisesViewModel>();
    return View(countCheckModel);
}

注意: 为了测试,我首次传递空列表,然后传递包含 3 个项目的列表。

视图:

@model List<DotNet6MVCWebApp.Controllers.HMOPremisesViewModel>

@{
    ViewData["Title"] = "Index";
}

@if(Model.Any())
{
    Console.WriteLine(Model.Count());
}
else{
   Console.WriteLine("else executed because model is zero");
}

输出:

检查 viewModel 是否等于 0 并显示 <div> 标签。

英文:

> I am trying to show a when checking model is 0. However, after
> creating a logic to check whether model is 0, i am getting white blank
> page. If the model is != 0 it is displaying the with the model != 0

Actually, Model.Count wouldn't help you get your desire result because in view we cannot count the overall model but its index element or items count in perticular. For instance, you can do like Model.FirstOrDefault().YourModelElements so if the list empty this block will not be executed.

However, the most easy and efficient way you can achieve that using Model.Any() which will execute if block if the list not empty other than it will execute else block.

Let's have a look in practice:

Controller:

public IActionResult Index()
        {
            var countCheckModel = new List&lt;HMOPremisesViewModel&gt;
            {

                new HMOPremisesViewModel(){ PremisesID = new Guid(), PremiseNickname = &quot;Microsoft&quot;,ApplicationType = &quot;A&quot;, ApplicationID =&quot;101&quot;,TaskType =1},
                new HMOPremisesViewModel(){ PremisesID = new Guid(), PremiseNickname = &quot;Amazon&quot;,ApplicationType = &quot;B&quot;, ApplicationID =&quot;201&quot;,TaskType =2},
                new HMOPremisesViewModel(){ PremisesID = new Guid(), PremiseNickname = &quot;Google&quot;,ApplicationType = &quot;C&quot;, ApplicationID =&quot;303&quot;,TaskType =2},
            };

            var emptyList = new List&lt;HMOPremisesViewModel&gt;();
            return View(countCheckModel);
        }

Note: For testing I am passing empty list at first time and then List with 3 items.

View:

@model List&lt;DotNet6MVCWebApp.Controllers.HMOPremisesViewModel&gt;

@{
    ViewData[&quot;Title&quot;] = &quot;Index&quot;;
}

@if(Model.Any())
{
    Console.WriteLine(Model.Count());
}
else{
   Console.WriteLine(&quot;else executed because model is zero&quot;);
}

Output:

检查 viewModel 是否等于 0 并显示 <div> 标签。

huangapple
  • 本文由 发表于 2023年5月15日 02:57:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定