【问题标题】:Default value for an input field on EF CoreEF Core 上输入字段的默认值
【发布时间】:2020-06-03 17:31:06
【问题描述】:

我正在尝试使用 EF Core MVC 应用程序,但遇到了障碍:

我有一个模型,它的 RenewDate 如下(摘录):

[Required]
[Display(Name = "Status")]
public char STATUS { get; set; }
[Required]
[Display(Name = "Licence Renew Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime RENEWDATE { get; set; }

我还有一个专门用于将 STATUS (char(1)) 转换为字符串的 LicenceViewModel,如下所示:

public class LicencaViewModel
{
    public Licence Licence { get; set; }
    public string Status 
    {
        get 
        {
            return Licence.STATUS switch
            {
                'P' => "Pending",
                'I' => "Inactive",
                'B' => "Blocked",
                'A' => "Active",
                _ => "Error"
            }; 
        }
        set 
        {
            Licence.STATUS = value switch
            {
                "Pending" => 'P',
                "Inactive" => 'I',
                "Blocked" => 'B',
                "Active" => 'A',
                _ => 'P'
            };
        }
    }
}

最后是 Create.cshtml 的相关行:

        <div class="form-group">
            <label asp-for="Licence.RENEWDATE" class="control-label"></label>
            <input asp-for="@DateTime.Now" readonly type="date" class="form-control" />
            <span asp-validation-for="Licence.RENEWDATE" class="text-danger"></span>
        </div>

所以,基本上我在这里想要实现的是一个只读字段,它简单地显示客户许可证的自动设置更新日期。现在的方式只是显示当前日期,但我不能使用@DateTime.Now.AddYears(1) 因为我得到Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions

我尝试在控制器上使用 ViewBag 和 ViewBag.AutoRenewDate = DateTime.Now.AddYears(1),以及在视图本身上使用 ViewData["AutoRenewDate"]。我也尝试将public DateTime RENEWDATE{ get { return DateTime.Now.AddYears(1); } } 直接添加到viewModel,也无济于事。

我已经看到很多使用纯 Html 从 ViewBags 和 ViewModels 获取日期的答案,但我想知道是否有办法使用剃须刀页面模板来做到这一点...

编辑:我忘了明确指出这是“创建”表单页面,所以我没有填充模型来设置 RENEWDATE。我需要一个只读表单字段,它会自动显示今天加上一年。

【问题讨论】:

  • 如果你只想显示起息日,你可以写@Html.DisplayFor(x =&gt; x.Licence.RENEWDATE)并在控制器中设置你想要的日期RENEWDATE = DateTime.Now.AddYears(1)
  • 我的错,我忘了明确指出这是“创建”页面,所以我没有填充模型来设置 RENEWDATE。
  • 您在视图模型中从状态字符到字符串的转换可能应该在从对象到视图模型的自动映射器映射中完成。 ViewModel 本身不应包含任何逻辑。
  • @TeaBaerd 我会记住这一点的!感谢您的提示!

标签: c# asp.net-mvc entity-framework razor


【解决方案1】:

我通过简单地忽略控制器脚本中的任何 POST 方法调用 viewmodel 并覆盖我需要在 POST 方法上设置为默认值的属性来解决这个问题,如下所示:

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Admin, manager")]
        public async Task<IActionResult> Create(LicencaViewModel licencaViewModel)
        {
            if (!ModelState.IsValid) return View(licencaViewModel);
            licencaViewModel.Licence.STATUS = 'P'; //This overrides whatever licencaViewModel currently has and sets it to the expected value.
            string serial = await _licencaViewModelService.InsertAsync(licencaViewModel.Licenca);
            return RedirectToAction("Details", new { id = serial });
        }

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2012-06-04
    • 2019-01-25
    • 1970-01-01
    • 2018-07-28
    • 2021-12-05
    • 2016-01-30
    • 1970-01-01
    • 2017-06-17
    相关资源
    最近更新 更多