猿问

如何在 ASP.NET 中使用视图模型?

我想查看名为 Nvram 和 ExecOut 的两个表的数据。


**Nvram:**


using Newtonsoft.Json;

using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations.Schema;


namespace SmartRouter.Domain

{

  public class Nvram

  {

    [Key, ForeignKey("SRouter")]

    public int NvramId { get; set; }

    [JsonProperty("wanConnectionMode")]

    public string ConnectionType { get; set; }

    [JsonProperty("WAN_MAC_ADDR")]

    public string IConfigMacAddress { get; set; }

    [JsonProperty("lan_gateway")]

    public string DefaultGateway { get; set; }


    [JsonProperty("wan_dhcp_hn")]

    public string HostName { get; set; }

    [JsonProperty("macCloneEnabled")]

    public string MacCloneEnable { get; set; }

    [JsonProperty("macCloneMac")]

    public string MacCloneMac { get; set; }

    [JsonProperty("wan_pppoe_user")]

    public string Username { get; set; }

    [JsonProperty("wan_pppoe_pass")]

    public string Password { get; set; }

    [JsonProperty("wan_pppoe_optime")]

    public string WanOperationMode { get; set; }

    [JsonProperty("wan_ipaddr")]

    public string WanIPAddress { get; set; }

    [JsonProperty("wan_netmask")]

    public string WanSubnetNetmask { get; set; }

    [JsonProperty("wan_gateway")]

    public string WanGatewayIP { get; set; }

    [JsonProperty("wan_primary_dns")]

    public string WanDNS1 { get; set; }

    [JsonProperty("wan_secondary_dns")]

    public string WanDNS2 { get; set; }

    [JsonProperty("lan_ipaddr")]

    public string LanIPAddress { get; set; }

    [JsonProperty("lan_netmask")]

    public string LanSubnetNetmask { get; set; }

    [JsonProperty("dhcpEnabled")]

    public bool? DHCPEnabled { get; set; }

    [JsonProperty("dhcpStart")]

    public string DHCPStart { get; set; }

    [JsonProperty("dhcpEnd")]

    public string DHCPEnd { get; set; }

    [JsonProperty("dhcpMask")]

    public string DHCPSubnetMask { get; set; }

    [JsonProperty("dhcpPriDns")]

    public string DHCPDNSPrimary { get; set; }

    [JsonProperty("dhcpSecDns")]

  }

}


喵喵时光机
浏览 145回答 1
1回答

呼如林

不确定哪个对象返回动态,因此您可能希望返回一些 DataFromRepoDto 对象(为该对象创建新类),其中分配了所有这些字段DataFromRepoDto:&nbsp; &nbsp; public class DataFromRepoDto{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string ConnectionType {get;set;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string IConfigMacAddress {get;set;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public string LanIPAddress {get;set;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //etc all needed fields with respective datatypes&nbsp; &nbsp; }&nbsp; &nbsp;您的方法已修改:public DataFromRepoDto GetRouterStatusByMac(string macAddress){&nbsp; var nvramdata=_srdbcontext.Nvrams.Where(q => q.SRouter.MacAddress == macAddress).Select(s => new&nbsp; {&nbsp; &nbsp; s.ConnectionType,&nbsp; &nbsp; s.IConfigMacAddress,&nbsp; &nbsp; s.LanIPAddress,&nbsp; &nbsp; s.LanSubnetNetmask,&nbsp; &nbsp; s.DefaultGateway&nbsp; }).FirstOrDefault();&nbsp; var execdata = _srdbcontext.ExeOuts.Where(q => q.SRouter.MacAddress == macAddress).Select(e => new&nbsp; {&nbsp; &nbsp; e.BuildInfo,&nbsp; &nbsp; e.Uptime,&nbsp; &nbsp; e.WANIPAddress,&nbsp; &nbsp; e.SubnetMask,&nbsp; &nbsp; e.DefaultGateway,&nbsp; &nbsp; e.PrimaryDNS,&nbsp; &nbsp; e.SecondaryDNS,&nbsp; &nbsp; e.LanMacAddress&nbsp; }).FirstOrDefault();&nbsp; return new DataFromRepoDto&nbsp; {&nbsp; &nbsp; ConnectionType = nvramdata.ConnectionType,&nbsp; &nbsp; IConfigMacAddress = nvramdata.IConfigMacAddress,&nbsp; &nbsp; LanIPAddress = nvramdata.LanIPAddress,&nbsp; &nbsp; //etc...&nbsp; };&nbsp; &nbsp; //return result;}然后在您的控制器操作中使用它。var dataFromRepo =&nbsp;routerrepository.GetRouterStatusByMac("f8:b5:68:a0:10:1c");var routerStatusViewModel = new RouterStatusViewModel{&nbsp; &nbsp; //object initializer&nbsp; &nbsp; WANIPAddress = dataFromRepo.WANIPAddress,&nbsp; &nbsp; //etc...};return View(routerStatusViewModel );之后,您可以像这样访问您的数据@model myproject.mynamespace.Models.RouterStatusViewModel<div>@Model.WANIPAddress<div>&nbsp;或使用 html 助手@Html.LabelFor(x => x.WANIPAddress )
随时随地看视频慕课网APP
我要回答