我已经检查了重复的问题,但没有找到任何解决方案......
我正在研究“献血者”项目,但在“捐赠者列表”视图中,我正面临“对象引用未设置为对象的实例”错误。 我已经花了两天时间解决这个问题,但还没有得到任何解决方案。
型号:
public class Donor
{
[Key]
public int DonorId { get; set; }
[Display(Name = "Donor Name")]
[StringLength(25,ErrorMessage="Your name should be less than 25 characters")]
[Required(ErrorMessage = "Enter your name")]
public string DonorName { get; set; }
[EmailAddress(ErrorMessage = "Invalid Pattern")]
public string Email { get; set; }
[Required(ErrorMessage = "Enter you address")]
public string Address { get; set; }
[Required(ErrorMessage = "Enter your password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Confirm your password")]
[Compare("Password", ErrorMessage = "please confirm your password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Enter your age")]
public int Age { get; set; }
[Display(Name = "Mobile No")]
[Required(ErrorMessage = "Enter you mobile number")]
[StringLength(11,ErrorMessage="Must be 11 digits number")]
public string MobileNo { get; set; }
public bool IsAble { get; set; }
public string Gender { get; set; }
[Display(Name = "Blood Group")]
public string BloodGroup { get; set; }
}
控制器:
Here i have a static list for "Blood Groups"....
public ActionResult DonorsList()
{
var BGList = new List<SelectListItem>
{
new SelectListItem{Text="A+ve",Value="1"},
new SelectListItem{Text="O+ve",Value="2"}
};
ViewBag.LList = new SelectList(BGList, "Value", "Text","1");
return View();
}
[HttpPost]
public ActionResult DonorsList(Donor model, string ddlBloodGroups)
{
using (DataAccess db = new DataAccess())
{
var BGList = new List<SelectListItem>
{
new SelectListItem{Text="A+ve",Value="1"},
new SelectListItem{Text="O+ve",Value="2"}
};
ViewBag.LList = new SelectList(BGList, "Value", "Text","1");
var obj = db.Donors.Where(m => m.BloodGroup == ddlBloodGroups).ToList();
return View(obj);
}
查看:
这就是我渲染下拉列表的方式......
@Html.DropDownList("ddlBloodGroups", ViewBag.LList as SelectList, "--Select--", new { @class = "form-control", style = "height:45px;font-size:18px;" })
<input type="submit" value="Search" class="btn" style="margin-top:5px;background-color:#3F4A5C;border:2px solid white" />
[HttpGet]请求效果很好......
但是当我通过选择DropDownList ...
为渲染html表添加以下代码时<div class="row">
<div class="col-md-12">
<table>
<tr>
<th>Donor Name</th>
@*<th>Email</th>
<th>Mobile No</th>
<th>Gender</th>
<th>Age</th>
<th>Address</th>
<th>Blood Group</th>*@
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.DonorName</td>
</tr>
}
</table>
</div>
</div>
...比它给我“对象引用未设置为对象的实例”错误在[HTTPGET]请求