提交时ViewModel属性为null

时间:2016-05-01 12:34:11

标签: c# asp.net-mvc viewmodel

我在视图中需要两个模型,所以我创建了ViewModel组合它们。这是获得方法。但是在帖子上,我的input type=submit正在返回ViewModel,但内部属性的值为null。有什么建议吗?

这些是我对Get and Post

的行为
[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();  
        }
    }

这是我的viewmodel:

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>

当我点击提交按钮时会显示这些条件。 my code

1 个答案:

答案 0 :(得分:3)

默认的模型绑定器不会绑定字段。请尝试使用属性

public class ReportZaPrikaz
{
    public KomentarZaPrikaz kzp { get; set; }
    public PrijavaKomentara report { get; set; }
}