mvc核心中的模型绑定问题

本文关键字:绑定 问题 模型 核心 mvc | 更新日期: 2023-09-27 18:07:20

我有一个POCO类,这个定义

public class IndOff
{
    public int Id { get; set; }
    [Display(Name = "Member Name")]
    [Required]
    public int MemberID { get; set; }
    public OfferingType OfferingType { get; set; }
    [Display(Name = "Offering Type")]
    [Required]
    public int OfferingTypeID { get; set; }
    public Currency Currency { get; set; }
    [Display(Name = "Currency")]
    [Required]
    public int CurrencyID { get; set; }
    public Member Member { get; set; }
    public double Amount { get; set; }
    public DateTime Date { get; set; }
}

然后我在这个类上定义了一个视图模型

public class IndOffViewModel
{
    public IndOffViewModel()
    {
    }
    public IEnumerable<Currency> Currencies { get; set; }
    public IEnumerable<OfferingType> OfferingTypes { get; set; }
    public Member Member { get; set; }
    public IndOff IndOff { get; set; }
}

从我的控制器中,捕获数据的视图被正确呈现,当我发布数据时出现问题。我的post action是

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Save(IndOff model, int memberId)
    {
        model.Date = DateTime.Today;
        if (!ModelState.IsValid)
        {
            var member = _context.Members.Single(m => m.Id == memberId);
            var viewModel = new IndOffViewModel
            {
                Member = member,
                Currencies = _context.Currencies.ToList(),
                OfferingTypes = _context.OfferingTypes.ToList()
            };
            return View("IndForm", viewModel);
        }
        if (model.Id == 0)
            _context.IndOffs.Add(model);
        else
        {
            var dbIndOff = _context.IndOffs.Single(c => c.Id == model.Id);
            dbIndOff.Amount = model.Amount;
            dbIndOff.CurrencyID = model.CurrencyID;
            dbIndOff.OfferingTypeID = model.OfferingTypeID;
            //dbIndOff.WorshipCenterID = weekOff.WorshipCenterID;
        }
        _context.SaveChanges();
        return RedirectToAction("New", new { id = model.Id});
    }

然而,传入的IndOff模型对象总是空的,属性为空。虽然当我使用chrome检查器时,我看到值已传递给控制器。问题是什么,我做错了什么?这就是View页面的样子

@model ViewModels.IndOffViewModel
@{ 
    ViewBag.Title = "Offering";
}
<form asp-action="Save" method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
    <label asp-for="IndOff.OfferingTypeID" class="control-label"></label>
    <select asp-for="IndOff.OfferingTypeID" asp-items="@(new SelectList(Model.OfferingTypes,"ID","Name"))" class="form-control">
        <option value="">-Select-</option>
    </select>
    <span asp-validation-for="IndOff.OfferingTypeID" class="text-danger" />
</div>
<div class="form-group">
    <label asp-for="IndOff.CurrencyID" class="control-label"></label>
    <select asp-for="IndOff.CurrencyID" asp-items="@(new SelectList(Model.Currencies,"Id","Name"))" class="form-control">
        <option value="">-Select-</option>
    </select>
    <span asp-validation-for="IndOff.CurrencyID" class="text-danger" />
</div>
<div class="form-group">
    <label asp-for="IndOff.Amount" class="control-label"></label>
    <input asp-for="IndOff.Amount" class="form-control" />
    <span asp-validation-for="IndOff.Amount" class="text-danger" />
</div>
@*<div class="checkbox">
    <label>
        <input asp-for="Member.Baptised" />
        Baptised ?
    </label>
</div>*@
<input type="hidden" asp-for="IndOff.Id" />
<input type="hidden" asp-for="Member.Id" name="memberId" />
<input type="submit" value="Submit" class="btn btn-primary" />
</form>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

mvc核心中的模型绑定问题

因为IndOff类包含在IndOffViewModel里面,你需要给动作一个"提示",关于在哪里开始使用Bind属性寻找。像这样:

public ActionResult Save([Bind(Prefix="IndOff")]IndOff model, int memberId) 

是否使用ViewModel来生成表单?在这种情况下,绑定将无法工作,因为属性的路径不匹配。

html输入标签的id将为"IndOff.{fieldName}"。

但是你的控制器接受IndOff而不是IndOffViewModel作为参数。IndOff属性的路径是"{fieldnName}"。

要解决这个绑定问题,你的ViewModel可以扩展IndOff而不是包含IndOff属性。