C# 在模型类中使用导航属性时出现空引用异常
我尝试在控制器操作中在数据库中添加新实体。C# 在模型类中使用导航属性时出现空引用异常,c#,asp.net-mvc,entity-framework,model,asp.net-mvc-viewmodel,C#,Asp.net Mvc,Entity Framework,Model,Asp.net Mvc Viewmodel,我尝试在控制器操作中在数据库中添加新实体。 这是我的模型课 public class Product { public int ProductID { get; set; } [Required(ErrorMessage = "Please enter product name")] public string Name { get; set; } [Required(ErrorMessage = "Please enter product m
这是我的模型课
public class Product
{
public int ProductID { get; set; }
[Required(ErrorMessage = "Please enter product name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter product model")]
public string Model { get; set; }
[Required(ErrorMessage = "Please enter product serial")]
public string Serial { get; set; }
[Required(ErrorMessage = "Please choose dealer")]
public int DealerID { get; set; }
[Required]
public Guid ClientID { get; set; }
[Required(ErrorMessage = "Please choose employee")]
public Guid EmployeeID { get; set; }
public virtual Dealer Dealer { get; set; }
public virtual Client Client { get; set; }
public virtual Employee Employee { get; set; }
[DisplayName("Commercial use")]
public bool UseType { get; set; }
}
这是在数据库中创建新产品的操作
public ViewResult Create()
{
PopulateDropDownLists();
var model = new Product();
return View(model);
}
[HttpPost]
public ActionResult Create(Product model)
{
try
{
if (ModelState.IsValid)
{
_repo.GetRepository<Product>().Add(model);
_repo.Save();
TempData["message"] = "Product was successfully created";
return RedirectToAction("List");
}
}
catch (DataException)
{
TempData["error"] =
"Unable to save changes. Try again, and if the problem persists, see your system administrator.";
return View("Error");
}
PopulateDropDownLists();
return View("Create");
}
我认为这是因为模型类具有虚拟属性,但无论如何,我不明白为什么在使用ViewModel时它是可以的。
有人能回答我吗?
提前Thx。虚拟属性不会改变结果。问题在于,视图是为了绑定到视图模型而编写的,因此接受模型是行不通的。如果要使用该模型;然后将视图绑定到模型。我认为您也应该将视图放在模型上!!那么,如果您从属性中删除“virtual”会发生什么呢?如果我删除virtual属性,我可以使用model类,并且不需要编辑ViewModelQuestion就可以了。当我使用模型类ViewCreate时,我将视图代码放在其中,ViewCreate具有适当的类型(产品)。使用ViewModel时,ViewCreate具有ProductViewModel类型。
@using System.Web.Mvc.Html
@model STIHL.WebUI.Models.Product
@using (Html.BeginForm())
{
@Html.EditorFor(m => m.Name)
@Html.EditorFor(m => m.Model)
@Html.EditorFor(m => m.Serial)
<div class="form-group">
@Html.LabelFor(m => m.DealerID, "Dealer")
@Html.DropDownListFor(m => m.DealerID, new SelectList((IEnumerable)TempData["Dealers"],"DealerID", "DealerNumber"), string.Empty, new {@class = "form-control"})
@Html.ValidationMessageFor(m => m.DealerID, null, new {@class = "help-block"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.EmployeeID, "Employee",new {@class = "control-label"})
@Html.DropDownListFor(m => m.EmployeeID, new SelectList((IEnumerable)TempData["Employees"],"EmployeeID", "FullName"),string.Empty, new {@class="form-control"})
@Html.ValidationMessageFor(m => m.EmployeeID, null, new {@class = "help-block"})
</div>
<div class ="ok-cancel-group">
<input class="btn btn-primary" type="submit" value="Create" />
@Html.ActionLink("Cancel", "List","Product",new {@class = "btn btn-primary"})
</div>
}
public class ProductViewModel
{
public Product Product { get; set; }
}