我只想使用Image以及其他学生信息进行CRUD操作。打开“编辑”页面后,我就知道了。
“编辑视图”的代码:
<div class="container-fluid">
<div class="row">
<div class="jumbotron jumbotron-fluid">
<div class="container">
<h1 class="display-4">ÖĞRENCİ DÜZENLEME SAYFASI</h1>
<p class="lead">Bu ekrandan öğrenci bilgilerini düzenleyebilirsiniz.</p>
</div>
</div>
<div class="btn btn-light">
@Html.ActionLink(" Öğrenci Listesine Geri Dön", "Index", null, new { @class = "btn btn-warning fa fa-list" })
</div>
@using (Html.BeginForm("Edit", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<hr />
<div class="col-md-5">
<div class="form-group">
<label>ALAN</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-graduation-cap"></i></span>
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Alanını Giriniz. Örneğin; Sayısal, Sözel</small>
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>SINIF</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-users"></i></span>
@Html.DropDownList("Class_Id", null, htmlAttributes: new { @class = "form-control" })
</div>
<small class="form-text text-muted">Öğrenci Sınıfını Seçiniz</small>
@Html.ValidationMessageFor(model => model.Class_Id, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>GRUP</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-indent"></i></span>
@Html.DropDownList("Group_Id", null, htmlAttributes: new { @class = "form-control" })
</div>
<small class="form-text text-muted">Öğrenci hazırlandığı sınav türünü seçiniz</small>
@Html.ValidationMessageFor(model => model.Group_Id, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>TC NUMARASI</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-id-card-o"></i></span>
@Html.EditorFor(model => model.TCno, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci TC Kimlik numarası giriniz</small>
@Html.ValidationMessageFor(model => model.TCno, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>AD</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-font"></i></span>
@Html.EditorFor(model => model.FName, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Ad</small>
@Html.ValidationMessageFor(model => model.FName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>SOYAD</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-strikethrough"></i></span>
@Html.EditorFor(model => model.LName, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Soyadı Giriniz</small>
@Html.ValidationMessageFor(model => model.LName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>CİNSİYET</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-intersex"></i></span>
<table>
<tr>
<td>@Html.RadioButtonFor(model => model.Gender, true) <strong>Bay</strong></td>
<td> </td>
<td>@Html.RadioButtonFor(model => model.Gender, false) <strong>Bayan</strong></td>
</tr>
</table>
</div>
<small class="form-text text-muted">Öğrenci Cinsiyeti seçiniz</small>
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>DOĞUM TARİHİ</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
@Html.EditorFor(model => model.Birthday, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Doğum Tarihi</small>
@Html.ValidationMessageFor(model => model.Birthday, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>ÖĞRENCİ TELEFON</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone"></i></span>
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Mobil telefon numarası giriniz. Ör: 05xx xxx xxxx</small>
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>DURUM</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-toggle-on"></i></span>
<table>
<tr>
<td>@Html.RadioButtonFor(model => model.Status, true) <strong>Aktif</strong></td>
<td> </td>
<td>@Html.RadioButtonFor(model => model.Status, false) <strong>Pasif</strong></td>
</tr>
</table>
</div>
<small class="form-text text-muted">Öğrenci Durumunu seçiniz</small>
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>FOTOĞRAF</label>
<div class="input-group">
<img src="@Url.Content(Session["Image"].ToString())" height="100" width="100" />
<br />
<input type="file" class="custom-file-input" value="Dosya Seç..." name="ImageFile">
</div>
<small class="form-text text-muted">Öğrenci fotoğrafı seçiniz. 1 MB küçük |.png|.jpg|.jpeg uzantılı dosyayı seçiniz.</small>
@Html.ValidationMessageFor(model => model.Photo, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-1"></div>
<div class="col-md-5">
<div class="form-group">
<label>E-POSTA</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
@Html.EditorFor(model => model.eMail, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Sisteme giriş yapabilmesi için bir E-Posta adresi giriniz.</small>
@Html.ValidationMessageFor(model => model.eMail, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>ŞİFRE</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-key"></i></span>
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Sisteme giriş yapabilmesi için bir şifre giriniz.</small>
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>KATILMA TARİHİ</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-check-circle-o"></i></span>
@Html.EditorFor(model => model.DateOfJoin, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Katılma tarihini giriniz.</small>
@Html.ValidationMessageFor(model => model.DateOfJoin, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>AYRILMA TARİHİ</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-circle-o"></i></span>
@Html.EditorFor(model => model.DateOfLeave, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci Ayrılma tarihini seçiniz.</small>
</div>
<div class="form-group">
<label>ADRES</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-map-marker"></i></span>
@Html.TextAreaFor(model => model.Address, new { @class = "form-control", @style = "height: 100px" })
</div>
<small class="form-text text-muted">Öğrenci Adres'ini giriniz.</small>
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>VELİ AD</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-id-badge"></i></span>
@Html.EditorFor(model => model.ParentName, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci'nin Veli adını giriniz.</small>
@Html.ValidationMessageFor(model => model.ParentName, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>VELİ TELEFON 1</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-mobile"></i></span>
@Html.EditorFor(model => model.ParentPhone1, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci'nin Veli Telefon numarasını giriniz.</small>
@Html.ValidationMessageFor(model => model.ParentPhone1, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>VELİ TELEFON 2</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone"></i></span>
@Html.EditorFor(model => model.ParentPhone2, new { htmlAttributes = new { @class = "form-control" } })
</div>
<small class="form-text text-muted">Öğrenci'nin Veli yedek telefon numarasını giriniz.</small>
</div>
<div class="form-group">
<label>VELİ CİNSİYET</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-intersex"></i></span>
<table>
<tr>
<td>@Html.RadioButtonFor(model => model.ParentGender, true) <strong>Bay</strong></td>
<td> </td>
<td>@Html.RadioButtonFor(model => model.ParentGender, false) <strong>Bayan</strong></td>
</tr>
</table>
</div>
<small class="form-text text-muted">Veli'nin cinsiyetini seçiniz.</small>
@Html.ValidationMessageFor(model => model.ParentGender, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<label>AÇIKLAMA</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-align-justify"></i></span>
@Html.TextAreaFor(model => model.Description, new { @class = "form-control", @style = "height: 100px" })
</div>
<small class="form-text text-muted">Öğrenci için bir açıklama giriniz.</small>
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-1"></div>
<br />
<br />
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-success ">
<i class="fa fa-floppy-o"> Değişiklikleri Kaydet</i>
</button>
</div>
</div>
<br />
<br />
}
</div>
学生控制者的代码:
private DBClassroomEntities db = new DBClassroomEntities();
/ GET: Student/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
ViewBag.Class_Id = new SelectList(db.Classes, "ID", "Name", student.Class_Id);
ViewBag.Group_Id = new SelectList(db.Groups, "ID", "Name", student.Group_Id);
return View(student);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Student student)
{ //does not enter here because I can't see the old data in Student.
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Class_Id = new SelectList(db.Classes, "ID", "Name", student.Class_Id);
ViewBag.Group_Id = new SelectList(db.Groups, "ID", "Name", student.Group_Id);
return View(student);
}
学生模型的代码
public partial class Student
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Student()
{
this.Discontinuities = new HashSet<Discontinuity>();
this.Grades = new HashSet<Grade>();
this.Payments = new HashSet<Payment>();
}
public int Id { get; set; }
public string Type { get; set; }
public Nullable<int> Class_Id { get; set; }
public Nullable<int> Group_Id { get; set; }
public string TCno { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public bool Gender { get; set; }
public Nullable<System.DateTime> Birthday { get; set; }
public string Phone { get; set; }
public string eMail { get; set; }
public string Password { get; set; }
public bool Status { get; set; }
public Nullable<System.DateTime> DateOfJoin { get; set; }
public Nullable<System.DateTime> DateOfLeave { get; set; }
public string Address { get; set; }
public string ParentName { get; set; }
public string ParentPhone1 { get; set; }
public string ParentPhone2 { get; set; }
public bool ParentGender { get; set; }
public string Photo { get; set; }
public string Description { get; set; }
//public HttpPostedFileBase filex { get; set; }
public virtual Class Class { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Discontinuity> Discontinuities { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Grade> Grades { get; set; }
public virtual Group Group { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Payment> Payments { get; set; }
}
和错误:
Screenshot of error (When I click the Edit page)
因此,我正在处理此错误一周。很抱歉输入长代码。
注意:如果我删除第180行中的div,则会在其他地方获取错误
感谢您的帮助。
更新: 默认情况下,它处于工作状态
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Class_Id, "Class_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Class_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Class_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Group_Id, "Group_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Group_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Group_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TCno, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TCno, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TCno, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Birthday, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Birthday, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Birthday, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.eMail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.eMail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.eMail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Status)
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateOfJoin, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfJoin, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateOfJoin, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateOfLeave, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfLeave, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateOfLeave, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ParentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ParentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ParentName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ParentPhone1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ParentPhone1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ParentPhone1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ParentPhone2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ParentPhone2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ParentPhone2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ParentGender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.ParentGender)
@Html.ValidationMessageFor(model => model.ParentGender, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Photo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Photo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Photo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
答案 0 :(得分:1)
未设置消息对象引用显示在该行代码中,但实际错误可能来自另一行。
请检查模型对象中的所有空大小写。您可以添加@if (Model != null){ }
换行到第180行
还从
更改下拉菜单的来源@Html.DropDownList("Class_Id", null, htmlAttributes: new { @class = "form-control" })
到
@Html.DropDownList("Class_Id", (SelectList)ViewBag.Class_Id , htmlAttributes: new { @class = "form-control" })
答案 1 :(得分:0)
我发现了问题。我检查模型中所有空值。
<img src="@Url.Content(Session["Image"].ToString())" height="100" width="100" />
此会话产生错误,因为未分配任何内容。这让我感到困惑,因为在其他地方都显示了错误。
Session["Image"] it's null
再次感谢:)