填充数据库中的下拉列表

时间:2011-09-03 01:24:06

标签: c# html asp.net-mvc database drop-down-menu

所以我正在编写一个基本的模拟网站(我的第一个),我正在尝试从数据库表中填充一个列表。

在我的控制器里,我有;

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();
    }
}

}

2 个答案:

答案 0 :(得分:1)

如果您的视图是强类型的,则可以使用帮助程序:

@Html.DropDownListFor(m => m.bloodtype, Model.bloodtypelist)

其中bloodtype是值,bloodtypelistList<T>,其中包含控制器中数据库中填充的所有值。

编辑:

尝试以下方法:

<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>

我猜它正在向视图传递一个空列表。

编辑:

在您的操作中,View();之前添加patientInit();

答案 1 :(得分:0)

如果您使用强类型视图,您的生活将变得更加轻松,但是您可以创建没有它们的下拉列表。看一下Html.DropDownList的签名

Html.DropDownList(
    string name,
    IEnumerable<SelectListItem> selectList,
    string optionLabel,
    object htmlAttributes)