【问题标题】:Assign the value of a property based on another property in model ASP.NET Core MVC 5基于模型 ASP.NET Core MVC 5 中的另一个属性分配属性的值
【发布时间】:2021-11-14 09:14:13
【问题描述】:

正如标题所说,我想要做的是基于另一个属性为模型的属性分配一个值。

在我的具体情况下,我想做的是将属性blongmonth(这是一个字符串,例如“January”)分配给bmonth(这是月份的数字,在这种情况下是1,是一个整数)。我必须使用这种类型,因为我正在处理无法修改的表。

问题是,我越努力,我就找不到可行的解决方案,而且在网上我找不到任何具体的东西可以帮助我解决我的小问题。当我在我的应用程序中(从下拉菜单中)选择月份 (blongmonth) 时,数据库中的月份编号 (bmonth) 始终为“0”。

我尝试在 ViewModel 中创建一个方法并在 Model 中调用它(这不是一个好主意)但不起作用,因为 ViewModel 仅用于呈现数据。现在我正在尝试直接在模型中做这件事:

public class Bdgfixmonth
{
    [Key]
    public int Counter { get; set; }
    [Required]
    public int Byear { get; set; }
    [Required]
    public string Bbudget { get; set; }
    
    [Required]
    //public int Bmonth { get; set; }
    private int _Bmonth;
    public int Bmonth 
    { 
        get
        {
             return _Bmonth;
        }
        set
        {
            if (Blongmonth == "January")
            {
                _Bmonth = 1;
            }
            else if (Blongmonth == "February")
            {
                _Bmonth = 2;
            }
            else if (Blongmonth == "March")
            {
                _Bmonth = 3;
            }
            else if (Blongmonth == "April")
            {
                _Bmonth = 4;
            }
            else if (Blongmonth == "May")
            {
                _Bmonth = 5;
            }
            else if (Blongmonth == "June")
            {
                _Bmonth = 6;
            }
            else if (Blongmonth == "July")
            {
                _Bmonth = 7;
            }
            else if (Blongmonth == "August")
            {
                _Bmonth = 8;
            }
            else if (Blongmonth == "September")
            {
                _Bmonth = 9;
            }
            else if (Blongmonth == "October")
            {
                _Bmonth = 10;
            }
            else if (Blongmonth == "November")
            {
                _Bmonth = 11;
            }
            else if (Blongmonth == "December")
            {
                _Bmonth = 12;
            }
            else
            {
                _Bmonth = 0;
            }
        }
    }
    
    [Required]
    public string Blongmonth 
    { get; set; }

    [Required]
    public int Closed { get; set; }

    [Required]
    public string Current { get; set; }
}

我也尝试将此代码放入Blongmonth 的getter 中,同时创建一个私有变量,但它不起作用。也试过这种方式:

if (Blongmonth == "January")
{
    _Bmonth = 1;
    _Bmonth = Bmonth;
}

这是创建视图中的下拉菜单:

<div class="form-group">
            <label asp-for="Blongmonth" class="control-label">@ViewBag.D</label>
            <select asp-for="Blongmonth" class="form-control" asp-action="">
                <option value="January">January</option>
                <option value="February">February</option>
                <option value="March">March</option>
                <option value="April">April</option>
                <option value="May">May</option>
                <option value="June">June</option>
                <option value="July">July</option>
                <option value="August">August</option>
                <option value="September">September</option>
                <option value="October">October</option>
                <option value="November">November</option>
                <option value="December">December</option>
            </select>
            <span asp-validation-for="Blongmonth" class="text-danger"></span>
        </div>

我也尝试过直接在 SQL Server 中创建触发器,但它不起作用:

SELECT 
    bmonth, 
    CASE
        WHEN blongmonth = 'January' THEN 1
        ELSE 'False'
    END
FROM bdgfixmonth

如果我在这个测试中选择一月,它将变为“假”。

我做错了什么?也许我必须在控制器中这样做?我是一个谦虚的初学者,如果比我更有经验的人可以帮助我,我将不胜感激。

【问题讨论】:

  • 您可以从 CultureInfo 类或 DateTime.ParseExact().Month 中获取月份名称;
  • 简单思考。将选项值设置为 1,2,3,4 等,为视图中选择框内的每个选项,当您在 DB 中插入/更新相应值时,将值转换为 INT 并保存。

标签: c# .net-core asp.net-core-mvc


【解决方案1】:

如果值完全由另一个字段确定,则不需要支持 _Bmonth 变量。您在 setter 代码中拥有的内容,将其移至 get 并返回应该可以工作。此外,如果您从未分配给它,则不需要集合:

public int Bmonth
    {
        get
        {
            if (Blongmonth == "January")
            {
                return 1;
            }
            else if (Blongmonth == "February")
            {
                return 2;
            }
            else if (Blongmonth == "March")
            {
                return 3;
            }
            else if (Blongmonth == "April")
            {
                return 4;
            }
            else if (Blongmonth == "May")
            {
                return 5;
            }
            else if (Blongmonth == "June")
            {
                return 6;
            }
            else if (Blongmonth == "July")
            {
                return 7;
            }
            else if (Blongmonth == "August")
            {
                return 8;
            }
            else if (Blongmonth == "September")
            {
                return 9;
            }
            else if (Blongmonth == "October")
            {
                return 10;
            }
            else if (Blongmonth == "November")
            {
                return 11;
            }
            else if (Blongmonth == "December")
            {
                return 12;
            }
            else
            {
                return 0;
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多