因此,我正在編寫一個基本的模擬網站(我的第一個),我試圖從數據庫表中填充一個列表。從數據庫中填充一個下拉列表
在我的控制器中,
public void patientInit()
{
hospitalSQLEntities db = new hospitalSQLEntities();
ViewData["bloodtypeList"] = db.bloodtypes.ToList();
ViewData["doctorno"] = "";
ViewData["wardno"] = "";
ViewData["patientid"] = "";
ViewData["patientname"] = "";
ViewData["address"] = "";
ViewData["gender"] = "";
ViewData["bloodtype"] = 0;
ViewData["spam"] = "";
ViewData["organs"] = "";
ViewData["formmessage"] = "";
}
而且在我的網頁我寫這個
<label for="bloodtype">Blood Type:</label>
<select name="bloodtype" >
<% List<InAmberClad.Models.bloodtype> bloodtypeList = (List<InAmberClad.Models.bloodtype>) ViewData["bloodtypeList"];
foreach (InAmberClad.Models.bloodtype st in bloodtypeList)
{
%>
<option value="<%= st.bloodcode%>" <%if (st.bloodcode==(String) ViewData["bloodtypeList"]) Response.Write("Selected"); %>>"><%Response.Write(st.meaning); %></option>
<% } %>
<option value="0" <%if ((Int32) ViewData["bloodtype"]==0) Response.Write("Selected"); %>></option>
</Select>
任何幫助,將不勝感激我一直停留幾個小時,它變得非常沮喪!
編輯:錯誤代碼圍繞着foreach循環,它表示「NullReferenceException未被用戶代碼處理,對象引用未設置爲對象的實例」。
編輯:這是我所有的控制器代碼;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InAmberClad.Models;
namespace InAmberClad.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult patiententry()
{
return View();
}
public void patientInit()
{
hospitalSQLEntities db = new hospitalSQLEntities();
ViewData["bloodtypeList"] = db.bloodtypes.ToList();
ViewData["doctorno"] = "";
ViewData["wardno"] = "";
ViewData["patientid"] = "";
ViewData["patientname"] = "";
ViewData["address"] = "";
ViewData["gender"] = "";
ViewData["bloodtype"] = 0;
ViewData["spam"] = "";
ViewData["organs"] = "";
ViewData["formmessage"] = "";
}
public void patientNewButtPressed()
{
if (Request.Params["submitter"] == "New Patient")
{
ViewData["doctorno"] = "";
ViewData["wardno"] = "";
ViewData["patientid"] = "(System Specified)";
ViewData["patientname"] = "";
ViewData["address"] = "";
ViewData["gender"] = "";
ViewData["bloodtype"] = 0;
ViewData["spam"] = "";
ViewData["organs"] = "";
ViewData["formmessage"] = "";
}
}
public void patientSearchByID()
{
}
public void patientSearchByName()
{
}
public void patientInsert()
{
hospitalSQLEntities db = new hospitalSQLEntities();
int newid = 0;
if (db.patients.Count() == 0)
newid = 1;
else
newid = db.patients.Max(u => u.patientid) + 1;
patient newpatient = new patient();
newpatient.patientid = newid;
newpatient.doctorno = Request.Params["doctorno"];
newpatient.wardno = Request.Params["wardno"];
newpatient.patientname = Request.Params["patientname"];
newpatient.address = Request.Params["address"];
newpatient.gender = Request.Params["gender"];
newpatient.bloodtype = Request.Params["bloodtype"];
newpatient.spam = Convert.ToInt32(Request.Params["spam"]);
newpatient.organs = Convert.ToInt32(Request.Params["organs"]);
db.AddTopatients(newpatient);
db.SaveChanges();
}
public void patientUpdate()
{
}
public ActionResult hospitalSQL()
{
patientInit();
if (Request.Params["submitter"] == "New Patient")
patientNewButtPressed();
else if (Request.Params["submitter"] == "Search")
{
if (Request.Params["searchpatientid"].Length > 0)
patientSearchByID();
else if (Request.Params["searchpatientname"].Length > 0)
patientSearchByName();
}
else if (Request.Params["submitter"] == "Cancel")
patientInit();
else if (Request.Params["submitter"] == "Save")
{
if (String.Compare(Request.Params["patientid"], "(System Specified)") == 0)
patientInsert();
else
patientUpdate();
}
return View();
}
}
}
究竟是什麼問題?如果您可以提供您收到的錯誤消息或描述現有行爲與所需行爲之間的差異,這將非常有用。 – Trott