mvc核心问题中的模型绑定

时间:2016-09-18 06:07:20

标签: c# asp.net-mvc

我有一个带有此定义的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; }
}

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

    [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");}
}

2 个答案:

答案 0 :(得分:0)

您是否使用ViewModel生成表单? 在这种情况下,绑定不会起作用,因为物业的路径并不匹配。

html输入标记的ID将是&#34; IndOff。{fieldName}&#34;。

但是你的控制器采用IndOff而不是IndOffViewModel作为参数。 IndOff属性的路径是&#34; {fieldnName}&#34;。

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

答案 1 :(得分:0)

由于IndOff内部包含IndOffViewModel类,因此您需要使用Bind属性为该操作提供“提示”,说明从哪里开始查看。像这样:

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