我有一个脚手架的“创建”视图,当我试图创建新记录时,返回错误:System.Web.Mvc.WebViewPage<TModel>.Model.get returned null
。甚至在我按下“保存”之前。
我的模特:
namespace Itucation.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("jobcoach")]
public partial class jobcoach
{
public int kursist_id { get; set; }
//[Column(TypeName = "text")]
public string note { get; set; }
[Key]
public int jobcoach_note_id { get; set; }
[Column("status")]
//[StringLength(50)]
[Display(Name = "Status")]
public string status { get; set; }
[Column(TypeName = "date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Success dato")]
public DateTime? succesDato { get; set; }
public int? AntalSamtaler { get; set; }
public virtual kursister kursister { get; set; }
}
}
我的控制器:
// GET: jobcoach/Create
public ActionResult Create(int? id)
{
ViewBag.kursist_id = new SelectList(db.kursister, "kursist_id", "fornavn");
ViewBag.status = new SelectList(db.JobcoachStatus, "status", "status");
ViewBag.ID = id;
ViewBag.kursist = (from k in db.kursister
where k.kursist_id == id
select k);
//var jc = db.jobcoach.Where(u => u.kursist_id == id).ToList().FirstOrDefault();
var jc = db.jobcoach.Where(r => r.kursist_id == id).ToList().FirstOrDefault();
if (jc == null)
{
// param is not set
}
else
{
return RedirectToAction("Edit/" + jc.jobcoach_note_id);
}
return View();
}
// POST: jobcoach/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "jobcoach_note_id,kursist_id,note,status,succesDato, AntalSamtaler")] jobcoach jobcoach)
{
if (ModelState.IsValid)
{
//Brugernavn (current user) skal også indsættes
jobcoach.note = jobcoach.note + " Af " + User.Identity.Name + " - " + DateTime.Now.ToString();
db.jobcoach.Add(jobcoach);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.kursist_id = new SelectList(db.kursister, "kursist_id", "fornavn", jobcoach.kursist_id);
return View(jobcoach);
}
我的观点:
@model Itucation.Models.jobcoach
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
<h4>jobcoach</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div>
@Html.LabelFor(model => model.kursist_id, "Kursist", htmlAttributes: new { @class = "control-label " })
<div>
@{
foreach (var navn in (ViewBag.kursist))
{
<text>Kursist ID: </text>@navn.kursist_id <br />
<text>Navn:</text> @navn.fornavn<text> </text> @navn.efternavn <br />
<text>E-mail: </text>@navn.mail
}
}
<input type="hidden" name="kursist_id" id="kursist_id" value="@ViewBag.ID" />
@*@Html.DropDownList("kursist_id", null, htmlAttributes: new { @class = "form-control" })*@
@Html.ValidationMessageFor(model => model.kursist_id, "", new { @class = "text-danger" })
</div>
</div>
<div>
@Html.LabelFor(model => model.note, htmlAttributes: new { @class = "control-label " })
<div>
@Html.TextAreaFor(model => model.note, new { @class = "form-control", @cols = "100%", @rows = "20", @tabindex = "21" })
@Html.ValidationMessageFor(model => model.note, "", new { @class = "text-danger" })
</div>
</div>
<div>
Status
<div>
<select id="status">
<option value="Uden status">Uden status </option>
<option value="Ordinær uddannelse">Ordinær uddannelse</option>
<option value="Deltidsjob">Deltidsjob</option>
<option value="Praktik">Praktik</option>
<option value="Løntilskud privat">Løntilskud privat</option>
<option value="Løntilskud offentlig">Løntilskud offentlig</option>
<option value="Ordinært job">Ordinært job</option>
</select>
</div>
</div>
<div>
Success dato
<div>
<input type="date" id="succesDato" name="succesDato" />
</div>
</div>
<div>
Antal samtaler
<div>
<input type="text" id="AntalSamtaler" name="AntalSamtaler" value="@Model.AntalSamtaler"/>
</div>
</div>
@*<div>
@Html.LabelFor(model => model.status, htmlAttributes: new { @class = "control-label " })
<div>
<select id="status" name="status" class="form-control ">
<option>Vælg evt. en status</option>
<option value="Ordinært job">Ordinært job</option>
<option value="Ordinær uddannelse">Ordinær uddannelse</option>
<option value="Deltidsjob">Deltidsjob</option>
<option value="Praktik">Praktik</option>
<option value="Løntilskud privat">Løntilskud privat</option>
<option value="Løntilskud offentlig">Løntilskud offentlig</option>
</select>
</div>
</div>*@
<div class="form-group col-md-12">
<div class="col-md-offset-2 col-md-1">
<input type="submit" value="Opret" class="btn btn-default" />
</div>
</div>
</div>
}
<div class="col-md-10">
@Html.ActionLink("Tilbage til listen", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我希望所有输入字段都有一个“空”视图,在那里我可以输入日期和命中的“保存”。相反,我得到了错误。
错误发生在@Html.ValidationMessageFor(model => model.note, "", new { @class = "text-danger" })
行。
我似乎无法弄清楚。我看到许多有关此错误的问题,但似乎没有一个适用于我的问题。
我将非常感谢您的帮助。
答案 0 :(得分:0)
这是因为您正在视图和Jobcoach类型的模型之间使用绑定。在视图中,如果要将视图绑定到模型,则必须通过以下方式将其作为参数传递给控制器的View()方法:
jobcoach jc=new jobcoach();
return View(jc);
您现在要执行的操作是从“空”模型渲染创建视图,因为您正在使用@Html模型类型的方法助手(LabelFor,ValidationMessageFor ....)
因此,当它试图将视图与模型绑定时,会抛出错误,因为它没有在视图中设置模型属性。
答案 1 :(得分:0)
我发现了问题:我试图访问@ Model.AntalSamtaler,但这是空的,因为我还没有保存它。我改变了这一行:
<input type="text" id="AntalSamtaler" name="AntalSamtaler" value="@Model.AntalSamtaler"/>
收件人:
<input type="text" id="AntalSamtaler" name="AntalSamtaler" value="1"/>
我通过在设置了@ Model.AntalSamtaler的“编辑”视图中进行复制/粘贴操作而犯了这个错误。