C# ASP.NET Core OnPost()加载数据时出现问题

C# ASP.NET Core OnPost()加载数据时出现问题,c#,html,razor,asp.net-core-mvc,C#,Html,Razor,Asp.net Core Mvc,我正在尝试将一个IEnumerable传递给我的OnPost()方法,但迄今为止我尝试的所有操作都只会导致它为null或计数为0。在OnGet()中,我将所需的所有数据都输入其中,然后我就可以在网页上很好地打印出来。以下是我的代码: EditLocations.cshtml: @page{assessmentId} @模型CustomerPageTest.Pages.View.EditLocationsModel @{ ViewData[“标题”]=“编辑位置”; } 编辑位置 身份证件 主

我正在尝试将一个
IEnumerable
传递给我的
OnPost()
方法,但迄今为止我尝试的所有操作都只会导致它为null或计数为0。在
OnGet()
中,我将所需的所有数据都输入其中,然后我就可以在网页上很好地打印出来。以下是我的代码:

EditLocations.cshtml:

@page{assessmentId}
@模型CustomerPageTest.Pages.View.EditLocationsModel
@{
ViewData[“标题”]=“编辑位置”;
}
编辑位置



身份证件 主办 数据中心 簇 地点名称 城市 陈述 带宽 @foreach(Model.locationView中的var位置) { @location.vHost\u id @位置。主机 @位置。数据中心 @位置.群集 @location.name @地点、城市 @位置、状态 @位置带宽 }
EditLocations.cshtml.cs:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using KelderModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CustomerPageTest.Pages.View
{
    public class EditLocationsModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public string SearchTerm { get; set; } //Search term for search bar

        [BindProperty(SupportsGet = true)]
        public IEnumerable<LocationView> LocationViews { get; set; }

        public int assessment_id { get; set; }

        public void OnGet(int assessmentId)
        {
            assessment_id = assessmentId;
            LocationViews = GetLocationViewsByAssessment(assessmentId);
            bool intOrString = false; //If false, its a string, if true, its an int
            try
            {
                int convertToInt = Int32.Parse(SearchTerm);
                intOrString = true;
            }
            catch (Exception) { }

            if (intOrString)
            { //Whole if statement is for the search bar, enables searching data in any column
                if (!IsNullOrEmpty(GetLocationById(Convert.ToInt32(SearchTerm))))
                    LocationViews = GetLocationById(Convert.ToInt32(SearchTerm));
                else if (!IsNullOrEmpty(GetLocationByBandwidth(Convert.ToInt32(SearchTerm))))
                    LocationViews = GetLocationByBandwidth(Convert.ToInt32(SearchTerm));
            }
            else
            {
                if (!IsNullOrEmpty(SearchLocationByHost(SearchTerm)))
                    LocationViews = SearchLocationByHost(SearchTerm);
                else if (!IsNullOrEmpty(SearchLocationByDatacenter(SearchTerm)))
                    LocationViews = SearchLocationByDatacenter(SearchTerm);
                else if (!IsNullOrEmpty(SearchLocationByCluster(SearchTerm)))
                    LocationViews = SearchLocationByCluster(SearchTerm);
                else if (!IsNullOrEmpty(SearchLocationByLocationName(SearchTerm)))
                    LocationViews = SearchLocationByLocationName(SearchTerm);
                else if (!IsNullOrEmpty(SearchLocationByCity(SearchTerm)))
                    LocationViews = SearchLocationByCity(SearchTerm);
                else if (!IsNullOrEmpty(SearchLocationByState(SearchTerm)))
                    LocationViews = SearchLocationByState(SearchTerm);
            }
        }

        public void OnPost()
        {
            foreach(var location in LocationViews)
            {
                location.isChecked = true;
            }
        }

        public IEnumerable<LocationView> GetLocationViewsByAssessment(int assessmentID) //Creates a list of locations based off of the locations in the database and assessment_id given
        {
            List<LocationView> returnList = new List<LocationView>();
            using (var context = new DataWarehouseContext())
            {
                List<int> vHostIDs = context.RvtoolsVHost //Gets list of vHostIDs mainly as a counter for the rows corresponding to the assessment_id
                    .Where(a => a.AssessmentId == assessment_id)
                    .Select(b => b.VHostId)
                    .ToList();

                foreach(int host in vHostIDs)
                {
                    LocationView temp = new LocationView();
                    temp.isChecked = false;
                    temp.assessment_id = assessmentID;
                    temp.vHost_id = host;

                    temp.location_id = context.Location //Returns LocationID from Location database table
                        .Where(a => a.VHostId == host)
                        .Select(b => b.LocationId)
                        .SingleOrDefault();

                    temp.name = context.Location //Returns Location Name from Location database table
                        .Where(a => a.VHostId == host)
                        .Select(b => b.Name)
                        .SingleOrDefault();

                    temp.city = context.Location //Returns City from Location database table
                        .Where(a => a.VHostId == host)
                        .Select(b => b.City)
                        .SingleOrDefault();

                    temp.state = context.Location //Returns State from Location database table
                        .Where(a => a.VHostId == host)
                        .Select(b => b.State)
                        .SingleOrDefault();

                    temp.bandwidth = context.Location //Returns City from Location database table
                        .Where(a => a.VHostId == host)
                        .Select(b => b.Bandwidth)
                        .SingleOrDefault();

                    temp.Host = context.RvtoolsVHost //returns Host Name from Host table using the vHostID that points at it
                        .Where(a => a.VHostId == host)
                        .Select(b => b.Name)
                        .FirstOrDefault();

                    temp.Datacenter = context.RvtoolsVHost //returns Cluster Name from Cluster table using the vHostID that points at it
                        .Where(a => a.VHostId == host)
                        .Select(b => b.Datacenter.Name)
                        .FirstOrDefault();

                    temp.Cluster = context.RvtoolsVHost //returns Cluster Name from Cluster table using the vHostID that points at it
                        .Where(a => a.VHostId == host)
                        .Select(b => b.VCluster.Name)
                        .FirstOrDefault();

                    returnList.Add(temp);
                }
            }
            return returnList;
        }

        public IEnumerable<LocationView> GetLocationById(int id) //Should only return a single location since ID is specific
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(id.ToString()) || c.location_id.ToString().Equals(id.ToString())
                   orderby c.bandwidth
                   select c;
        }

        public IEnumerable<LocationView> GetLocationByBandwidth(int id) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(id.ToString()) || c.bandwidth.ToString().StartsWith(id.ToString())
                   orderby c.bandwidth
                   select c;
        }

        public IEnumerable<LocationView> SearchLocationByHost(string name = null) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(name) || c.Host.StartsWith(name)
                   orderby c.Host
                   select c;
        }

        public IEnumerable<LocationView> SearchLocationByDatacenter(string name = null) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(name) || c.Datacenter.StartsWith(name)
                   orderby c.Datacenter
                   select c;
        }

        public IEnumerable<LocationView> SearchLocationByCluster(string name = null) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(name) || c.Cluster.StartsWith(name)
                   orderby c.Cluster
                   select c;
        }

        public IEnumerable<LocationView> SearchLocationByLocationName(string name = null) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(name) || c.name.StartsWith(name)
                   orderby c.name
                   select c;
        }

        public IEnumerable<LocationView> SearchLocationByCity(string name = null) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(name) || c.city.StartsWith(name)
                   orderby c.city
                   select c;
        }

        public IEnumerable<LocationView> SearchLocationByState(string name = null) //Can return many locations
        {
            return from c in LocationViews
                   where string.IsNullOrEmpty(name) || c.state.StartsWith(name)
                   orderby c.state
                   select c;
        }



        public bool IsNullOrEmpty<LocationView>(IEnumerable<LocationView> enumerable)
        {
            return enumerable == null || !enumerable.Any();
        }

    }
}
public class EditLocationsModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public IEnumerable<LocationView> LocationViews { get; set; }
        public ActionResult OnGet()
        {
            List < LocationView > list = new List<LocationView>();
            LocationView l1 = new LocationView { isChecked = false, vHost_id = 1, city = "city1", Host = "h1", Cluster = "c1", Datacenter = "d1", name = "location1", state = "state1", bandwidth = "b1" };
            LocationView l2 = new LocationView { isChecked = true, vHost_id = 2, city = "city2", Host = "h2", Cluster = "c2", Datacenter = "d2", name = "location2", state = "state2", bandwidth = "b2" };
            list.Add(l1);
            list.Add(l2);
            LocationViews = list;
            return Page();

        }
        public void OnPost() {
          
        }
    }
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Threading.Tasks;
使用凯尔德模型;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
命名空间CustomerPageTest.Pages.View
{
公共类EditLocationsModel:PageModel
{
[BindProperty(SupportsGet=true)]
公共字符串搜索项{get;set;}//搜索栏的搜索项
[BindProperty(SupportsGet=true)]
公共IEnumerable LocationViews{get;set;}
公共整数评估_id{get;set;}
公共网络(内部评估ID)
{
评估id=评估id;
LocationViews=GetLocationViewsByAssessment(评估ID);
bool intOrString=false;//如果为false,则为字符串;如果为true,则为int
尝试
{
int convertToInt=Int32.Parse(SearchTerm);
intOrString=true;
}
捕获(异常){}
如果(输入字符串)
{//Whole if语句用于搜索栏,可在任何列中搜索数据
如果(!IsNullOrEmpty(GetLocationById(Convert.ToInt32(SearchTerm)))
LocationViews=GetLocationById(Convert.ToInt32(SearchTerm));
如果(!IsNullOrEmpty(GetLocationByBandwidth(Convert.ToInt32(SearchTerm))),则为else
LocationViews=GetLocationByBandwidth(转换为32(SearchTerm));
}
其他的
{
如果(!IsNullOrEmpty(SearchLocationByHost(SearchTerm)))
LocationViews=SearchLocationByHost(SearchTerm);
如果(!IsNullOrEmpty(SearchLocationByDatacenter(SearchTerm)),则为else
LocationViews=SearchLocationByDatacenter(SearchTerm);
如果(!IsNullOrEmpty(SearchLocationByCluster(SearchTerm)),则为else
LocationViews=SearchLocationByCluster(SearchTerm);
如果(!IsNullOrEmpty(SearchLocationByLocationName(SearchTerm)),则为else
LocationViews=SearchLocationByLocationName(SearchTerm);
如果(!IsNullOrEmpty(SearchLocationByCity(SearchTerm)),则为else
LocationViews=SearchLocationByCity(SearchTerm);
如果(!IsNullOrEmpty(SearchLocationByState(SearchTerm)),则为else
LocationViews=SearchLocationByState(SearchTerm);
}
}
邮政署公告
{
foreach(LocationView中的变量位置)
{
location.isChecked=true;
}
}
public IEnumerable GetLocationViewsByAssessment(int-assessmentID)//根据数据库中的位置和给定的评估id创建位置列表
{
List returnList=新列表();
使用(var context=newdatawarehousecontext())
{
List vHostIDs=context.RvtoolsVHost//获取vHostIDs列表,主要作为与评估id对应的行的计数器
.其中(a=>a.AssessmentId==评估id)
.选择(b=>b.VHostId)
.ToList();
foreach(vHostIDs中的int主机)
{
LocationView温度=新LocationView();
温度检查=假;
临时评估id=评估id;
temp.vHost_id=主机;
temp.location\u id=context.location//从位置数据库表返回位置id
.其中(a=>a.VHostId==主机)
.选择(b=>b.LocationId)
.SingleOrDefault();
temp.name=context.Location//从位置数据库表返回位置名称
.其中(a=>a.VHostId==主机)
.选择(b=>b.Name)
.SingleOrDefault();
temp.city=context.Location//从位置数据库表返回城市
.其中(a=>a.VHostId==主机)
.选择(b=>b.City)
public class EditLocationsModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public IEnumerable<LocationView> LocationViews { get; set; }
        public ActionResult OnGet()
        {
            List < LocationView > list = new List<LocationView>();
            LocationView l1 = new LocationView { isChecked = false, vHost_id = 1, city = "city1", Host = "h1", Cluster = "c1", Datacenter = "d1", name = "location1", state = "state1", bandwidth = "b1" };
            LocationView l2 = new LocationView { isChecked = true, vHost_id = 2, city = "city2", Host = "h2", Cluster = "c2", Datacenter = "d2", name = "location2", state = "state2", bandwidth = "b2" };
            list.Add(l1);
            list.Add(l2);
            LocationViews = list;
            return Page();

        }
        public void OnPost() {
          
        }
    }