Reputation: 35
So I am writing a basic mock website (my first one) and I am trying to populate a list from a database table.
In my controller I have;
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"] = "";
}
And in my page I've written this
<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>
Any help with this would be greatly appreciated I've been stuck for hours and it's getting super frustrating!
Edit: The error code is surrounding the foreach loop, it says that the "NullReferenceException was unhandled by the user code. Object reference not set to an instance of an object."
Edit: Here is all of my controller code;
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();
}
}
}
Upvotes: 1
Views: 429
Reputation: 12437
If your view is strongly typed, you can use a helper:
@Html.DropDownListFor(m => m.bloodtype, Model.bloodtypelist)
Where bloodtype
is the value and bloodtypelist
is a List<T>
that holds all the values populated from your db in your controller.
Edit:
Try the following:
<select name="bloodtype">
<% foreach (var st in (List<InAmberClad.Models.bloodtype>)ViewData["bloodtypeList"]) { %>
<option value="<%=st.bloodcode%>" <%if (st.bloodcode==(String)ViewData["bloodtypeList"]){%> selected<%}%>><%=stmeaning%></option>
<% } %>
<option value="0" <%if ((Int32)ViewData["bloodtype"]==0){%> selected<%}%>></option>
</select>
I'm guessing it's passing an empty list to the view.
Edit:
In your action, before View();
add patientInit();
Upvotes: 1
Reputation: 23070
Your life will become much easier if you would use strongly typed views, however you can create a drop list without them. Take a look at the signature for Html.DropDownList
Html.DropDownList(
string name,
IEnumerable<SelectListItem> selectList,
string optionLabel,
object htmlAttributes)
Upvotes: 0