我正在尝试从dropdownlistfor获取值,我在运行代码时可以在dropdownlist中看到值,但是当我提交时,它会抛出错误 “ System.NullReferenceException:'对象引用未设置为对象的实例。' System.Web.Mvc.WebViewPage.Model.get返回null。“
这并不总是发生。当我在重新打开应用程序后第一次运行时,它可以正常工作,但是在那之后,即使我没有更改代码,它也给了我这个错误。
@model ProjectDentalweb.Models.Viewmodel.Makeappointment
@{
ViewBag.Title = "Schedule";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.demo</h2>
<h2>@ViewBag.demo2</h2>
<h2>appointment</h2>
@using (Html.BeginForm("Schedule", "Patient"))
{
@Html.AntiForgeryToken()
<h2>@ViewBag.demo</h2>
<h2>@ViewBag.demo2</h2>
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.appointments.PatientName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBox("PatientName", (string)ViewBag.Uname, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.appointments.PatientName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.appointments.AppointmentDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.appointments.AppointmentDate, "{0: d mm yyyy}", new { @class = "form-control datepick" })
@Html.ValidationMessageFor(m => m.appointments.AppointmentDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.appointments.Did, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.appointments.Did, new SelectList(Model.doctors, "DoctorId", "DoctorName"), "Select Doctor", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.appointments.Did, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.appointments.Reason, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.appointments.Reason, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.appointments.Reason, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.appointments.AppointmentStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.appointments.AppointmentStatus, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.appointments.AppointmentStatus, "", new { @class = "text-danger" })
</div>
</div>
</div>
<button type="submit" class="btn-primary">Create</button>
}
控制器//
[HttpGet]
public ActionResult Schedule()
{
if (Session["Username"] != null)
{
var x = Session["Username"];
var U = _context.Patients.Single(m => m.UserLogin.UserName == (string)x).FirstName;
ViewBag.Uname = U;
//ViewBag.Dlist = _context.Doctors.ToList();
var Viewmodel = new Makeappointment // Get doctor info in dropdownlist for appointment table
{
doctors = _context.Doctors.ToList()
};
return View("Schedule",Viewmodel);
}
else
{
return RedirectToAction("Signin", "Useraccess");
}
}
// POST : Patient / Schedule
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Schedule(Makeappointment makeappointment)
{
if (ModelState.IsValid)
{
var demo = Session["Username"];
var U = _context.Patients.Single(m => m.UserLogin.UserName == (string)demo);
int Uid = Convert.ToInt32(U.PatientId);
Appointment appointment = new Appointment();
appointment.Pid = Uid;
appointment.PatientName = makeappointment.appointments.PatientName;
appointment.AppointmentDate = makeappointment.appointments.AppointmentDate;
appointment.Did = makeappointment.appointments.Did.Value;
appointment.Reason = makeappointment.appointments.Reason;
appointment.AppointmentStatus = false;
_context.Appointments.Add(appointment);
_context.SaveChanges();
return RedirectToAction("Appointment", "Patient");
}
else
{
}
return View();
}
}
<--------------------------------------------> < / p>
Viewmodel //
命名空间ProjectDentalweb.Models.Viewmodel {
public class Makeappointment
{
public Appointment appointments { get; set; }
public IList<Doctor> doctors { get; set; }
}
}
答案 0 :(得分:0)
此错误可能是由于尝试在剃刀视图中使用空模型引起的。在这种情况下,使用前请检查模型是否为空,如下所示:
ctypes.DEFAULT_MODE