提交时,ViewModel属性为null

我的视图中需要两个模型,因此我创建了将它们组合在一起的ViewModel.那就是获取方法.但是在Post上,我的输入type = submit返回ViewModel,但是内部的属性值为null.有什么建议吗?

这些是我获取和发布的动作

[HttpGet]
    public ActionResult Report(int id)
    {
        using (SkolskaBibliotekaDBModel db = new SkolskaBibliotekaDBModel())
        {
            ReportZaPrikaz RZP = new ReportZaPrikaz();

            var komentar = db.Komentar.FirstOrDefault(x => x.KomentarID == id);

            RZP.kzp = new KomentarZaPrikaz()
            {
                KomentarID = komentar.KomentarID,
                KomentarText = komentar.KomentarText,
                KorisnikID = komentar.KorisnikID,
                Reported = komentar.Reported,
                VremeKomentara = komentar.VremeKomentara,
                Ime = komentar.Korisnik.Ime,
                Prezime = komentar.Korisnik.Prezime,
                PicPath = komentar.Korisnik.PicPath,
                VoteUp = komentar.VoteUp,
                VoteDown = komentar.VoteDown
            };

            RZP.report = db.PrijavaKomentara.Create();

            return View("Report",RZP);
        }
    }

    [HttpPost]
    public void Report(ReportZaPrikaz RZP)
    {
        using (SkolskaBibliotekaDBModel db = new SkolskaBibliotekaDBModel())
        {
            db.PrijavaKomentara.Add(RZP.report);
            db.SaveChanges();  
        }
    }

这是我的视图模型:

namespace SkolskaBiblioteka.ViewModels
{
    public class ReportZaPrikaz
    {

        public KomentarZaPrikaz kzp;
        public PrijavaKomentara report;

    }
}

这是我的看法:

@model SkolskaBiblioteka.ViewModels.ReportZaPrikaz

@using SkolskaBiblioteka.ViewModels

@{
    ViewBag.Title = "Пријава коментара";
}

<div class="container">
@Html.Partial("_Komentar", Model.kzp)

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>PrijavaKomentara</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @{
            Model.report.KorisnikId = ((KorisnikZaPrikaz)Session["Korisnik"]).KorisnikId;
        }
        @Html.HiddenFor(x => x.report.KorisnikId)

        @{
            Model.report.KomentarId = Model.kzp.KomentarID;
        }
        @Html.HiddenFor(x => x.report.KomentarId)

        @{
            Model.report.VremePrijave = DateTime.Now;
        }
        @Html.HiddenFor(x => x.report.VremePrijave)

        <div class="form-group">
            @Html.LabelFor(model => model.report.Tekst, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.report.Tekst, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
</div>

当我点击提交按钮时,这些条件就会显示出来.
提交时,ViewModel属性为null

解决方法:

默认的model-binder不会绑定字段.尝试改用属性:

public class ReportZaPrikaz
{
    public KomentarZaPrikaz kzp { get; set; }
    public PrijavaKomentara report { get; set; }
}
上一篇:CodeGo.net>如何使用ViewModels提供动态数据集到单个视图


下一篇:MVC模式和MVVM模式简单理解