簡體   English   中英

ASP.NET MVC項目的理想模型結構,用於處理多個局部視圖

[英]Ideal model structure for an ASP.NET MVC project for handeling multiple partial views

我有兩個名為_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; }
    }
}

在這里,您可以看到我造成的混亂。 我的問題是在同一控制器中使用多個模型時如何處理多個模型?

在這里,我沒有在一個類中創建所有數據,因為我希望根據需要重用它們。

如何處理模型中的數據? 這是我在項目中創建局部視圖時遇到的主要問題。

您可以一起使用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

經過一番努力,我得到了結果。

 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

        }

是的,這很容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM