【问题标题】:Ideal model structure for an ASP.NET MVC project for handeling multiple partial views用于处理多个局部视图的 ASP.NET MVC 项目的理想模型结构
【发布时间】:2016-12-29 06:37:47
【问题描述】:

我有两个名为 _centerDetails.cshtml_centerRights.cshtml 的局部视图。

当我单击提交时,我正在从centerdetails 传递数据,并希望在切换另一个部分视图时显示此数据,但没有将其发布到服务器。

这是我创建的模型类,用于通过以下方式处理我的数据 控制器:

namespace ADWP_AdminWebPortal.Models
{

    public class CountryList
    {
        [Required(ErrorMessage = "Select a Country.")]
        public int CountryId { get; set; }
        [Required]
        public string Country { get; set; }

        [Required(ErrorMessage = "Select a State.")]
        public int StateId { get; set; }
        [Required]
        public string State { get; set; }

        [Required(ErrorMessage = "Select a City.")]
        public int CityId { get; set; }
        [Required]
        public string City { get; set; }
    }

    public class CustomerDetails: CountryList
    {
        public int ClientId { get; set; }

        [Required (ErrorMessage="Eneter the First name")]
        [DataType(DataType.Text)]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Eneter the Middle name")]
        [DataType(DataType.Text)]
        public string MiddleName { get; set;}
        public string NatureOfOccupation { get; set; }

        public string AgentId { get; set; }

        [Required(ErrorMessage ="Select a Client Type.")]
        public string ClientType { get; set; }

        public int TariffId { get; set; }
        public string TariffName { get; set; }
        public int ServiceId { get; set; }
        public string ServiceName { get; set; }
        public string OrderId { get; set; }
        public int PaymentMethodId { get; set; }
        public string PaymentMethodName { get; set; }
    }
}

在这里你可以看到我造成的混乱。我的问题是当您在同一个控制器中处理多个模型时如何处理多个模型?

这里我没有在一个类中创建所有数据,因为我希望它可以根据我的需要重用。

如何处理模型中的数据?这是主要的问题 当我在我的项目中创建部分视图时来找我。

【问题讨论】:

  • 预期输出是什么?
  • 您的表中没有重复记录!您是否正在考虑将您的表格转换为符合第二范式的格式?
  • 在您的示例中,Robin 出现了两次,但有 2 个不同的成本(不重复),您是否想要最大成本?在这种情况下,只需使用 MAX(),并按名称和类型分组
  • 你为什么不编辑你的问题来反映这一点
  • 请编辑您的问题,否则我将不得不投反对票

标签: asp.net-mvc model-view-controller partial-views


【解决方案1】:

您可以同时使用 CTE 和 ROW_NUMBER 函数从表中删除重复的行。

With CTE AS (
    SELECT VERIFICATIONTYPE,
            NAME,
            COST,
            RN = ROW_NUMBER() OVER (PARTITION BY VERIFICATIONTYPE, NAME, COST ORDER BY VERFICATIONTYPE)
    FROM DETAILS)
DELETE FROM CTE WHERE RN > 1
END

【讨论】:

  • 为什么你认为这行不通?你能解释一下吗? @rajan-mishra
【解决方案2】:

经过一番挣扎,我得到了结果。

 public class CountryList
    {
        [Required(ErrorMessage = "Select a Country.")]
        public int CountryId { get; set; }
        [Required]
        public string Country { get; set; }
    }

    // Added Class

public class State
{
      [Required(ErrorMessage = "Select a State.")]
        public int StateId { get; set; }
        [Required]
        public string State { get; set; }
}
    // Added Class
public class City
{
      [Required(ErrorMessage = "Select a City.")]
        public int CityId { get; set; }
        [Required]
        public string City { get; set; }
}

    public class CustomerDetails
    {
        public int ClientId { get; set; }

        [Required (ErrorMessage="Enter the first name")]
        [DataType(DataType.Text)]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Enter the middle name")]
        [DataType(DataType.Text)]
        public string MiddleName { get; set; }


        public int TariffId { get; set; }
        public string TariffName { get; set; }
        public int ServiceId { get; set; }
        public string ServiceName { get; set; }
        public string OrderId { get; set; }
        public int PaymentMethodId { get; set; }
        public string PaymentMethodName { get; set; }
        public List<CountryList> { get; set; } //added list
        public List<State> { get; set; } //added list
        public List<City> { get; set; } //added list

        }

是的,就是这么简单。

【讨论】:

    猜你喜欢
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多