请帮我修复此错误。 我经历了这么多的艺术品,但我没有找到同样的产品。
单击“提交”按钮时会出现此问题。
对象引用未设置为对象的实例。描述: 在执行当前期间发生了未处理的异常 网络请求。请查看堆栈跟踪以获取更多信息 错误以及它在代码中的起源。
异常详细信息:System.NullReferenceException:对象引用 没有设置为对象的实例。
我的代码如下:
股票类别:
public class Stock
{
//[Required(ErrorMessage = "Please Select Instrument Code")]
[Display(Name = "Instrument Code :")]
public string instrumentCode { get; set; }
public IEnumerable instrumentCodeList { get; set; }
}
控制器代码:
public ActionResult StockEntry()
{
List<Instrument_master> instrrmstr = new List<Instrument_master>();
using (NSDLEntities db = new NSDLEntities())
{
instrrmstr = db.Instrument_master.ToList();
}
var model = new Stock
{
instrumentCodeList=new SelectList(instrrmstr,"im_instcd","im_desc"),
instrumentCode = "0"
};
return View(model);
}
查看代码:
@using (Html.BeginForm("StockEntrySubmit", "Home",FormMethod.Post))
{
@Html.LabelFor(m => m.instrumentCode)
@Html.DropDownListFor(m => m.instrumentCode, Model.instrumentCodeList, "-Select-")
@Html.ValidationMessageFor(m => m.instrumentCode)
}
此问题已在View中出现,即在raser中。所以这不是一个重复的问题。