2012-07-29 112 views
1

下方是我的視圖,其中包含一個下拉列表。未將對象引用設置爲MVC3中的對象實例

@model CommerceSuite.Web.Models.RelocateStock.RelocateStockModel 

@Html.ValidationSummary(true) 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.products) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(model=>model.ProductId,Model.products,"Select Product")<br /> 
     @Html.ValidationMessageFor(model => model.ProductId) 
    </div> 

這是我的模型類。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.ComponentModel.DataAnnotations; 
using CommerceSuite.Web.Models.LocationDetail; 
using CommerceSuite.Web.Models.Product; 

namespace CommerceSuite.Web.Models.RelocateStock 
{ 
public class RelocateStockModel 
{ 
    public RelocateStockModel() 
    { 
     products = new List<SelectListItem>(); 
     Fromlocations = new List<SelectListItem>(); 
     Tolocations = new List<SelectListItem>(); 
     product = new ProductModel(); 
     locationDetail = new LocationDetailModel(); 
    } 

    [HiddenInput(DisplayValue = false)] 
    public long ProductStockId { get; set; } 

    public long ProductId { get; set; } 

    [Display(Name = "Product Id")] 
    [Required] 
    public IList<SelectListItem> products { get; set; } 

    public long LocationFromId { get; set; } 

    [Display(Name = "Location From")] 
    [Required] 
    public IList<SelectListItem> Fromlocations { get; set; } 


    public long LocationToId { get; set; } 

    [Display(Name = "Location To")] 
    [Required] 
    public IList<SelectListItem> Tolocations { get; set; } 

    [Display(Name="Quantity Shifted")] 
    public long Quantity { get; set; } 

    public LocationDetailModel locationDetail { get; set; } 
    public ProductModel product { get; set; } 

} 
} 

這是我的控制器。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using CommerceSuite.Services; 
using CommerceSuite.Web.Models.Product; 
using CommerceSuite.Data.Models; 
using CommerceSuite.Web.Models.RelocateStock; 

namespace CommerceSuite.Web.Controllers 
{ 

public class RelocateStockController : BaseCsController 
{ 
    private readonly IProductService _product; 
    private readonly ILocationDetailService _locationDetail; 
    private readonly IProductStockService _productStock; 

    public RelocateStockController(IProductService product, ILocationDetailService locationDetail, IProductStockService productStock) 
    { 
     this._productStock = productStock; 
     this._product = product; 
     this._locationDetail = locationDetail; 
    } 
    // 
    // GET: /RelocateStock/ 

    private List<SelectListItem> PopulateProductId() 
    { 
     var productCollection = new SelectList(_product.GetAllProduct(), "ProductId", "Name").ToList(); 
     return productCollection; 
    } 

    private List<SelectListItem> PopulateLocation() 
    { 
     var locationCollection = new SelectList(_locationDetail.GetAllLocationDetail(), "LocationId", "Name").ToList(); 
     return locationCollection; 
    } 

    public ActionResult Index() 
    { 
     return PartialView("_RelocateStockView"); 
    } 

    [HttpGet] 
    public ActionResult StockRelocate() 
    { 
     var model = new RelocateStockModel(); 
     model.products = PopulateProductId(); 
     model.Fromlocations = PopulateLocation(); 
     model.Tolocations = PopulateLocation(); 
     return PartialView("_RelocateStockView"); 
    } 

    [HttpPost] 
    public ActionResult StockRelocate(RelocateStockModel model) 
    { 
     CommerceSuiteWMDBContext context = new CommerceSuiteWMDBContext(); 

     var productStockId = (from ProductStock in context.ProductStocks 
          where (ProductStock.ProductId == model.ProductId) && (ProductStock.LocationId == model.LocationFromId) 
          select ProductStock.ProductStockId).SingleOrDefault(); 

     var proStocksId = (from ProductStock in context.ProductStocks 
         where (ProductStock.ProductId == model.ProductId) && (ProductStock.LocationId == model.LocationToId) 
         select ProductStock.ProductStockId).SingleOrDefault(); 

     var productStock = _productStock.GetProductStockById(productStockId); 

     var ProStock = new ProductStock(); 
     ProStock.ProductId = model.ProductId; 
     var qty = model.Quantity; 
     ProStock.LocationId = model.LocationFromId; 
     ProStock.PackageId = 10000; 
     ProStock.QuantityOnHand = ProStock.QuantityOnHand - qty; 
     _productStock.UpdateProductStock(ProStock); 
     SuccessNotification("Edited Successfully"); 
     return RedirectToAction("Index"); 
    } 
} 

} 

,當我嘗試運行我的代碼在@Html.DropDownListFor得到了在我看來,一個錯誤...... 它說:不設置到對象的實例

對象引用。

這是什麼意思?

+0

請告訴我們您的控制器。我懷疑你的班級沒有在你的控制器中初始化。確保你的控制器中有如下內容: var m = new RelocateStockModel(); return View(m); – jzm 2012-07-29 23:45:50

+0

我在我的控制器中完成了它。 – 2012-07-30 08:51:00

+0

我把我的控制器... – 2012-07-30 08:54:19

回答

3

你不顯示你的控制器,所以我想,你不會發送你的模型來查看。 嘗試在你的控制器使用這種操作方法:

public ActionResult Index() 
{ 
    return View(new RelocateStockModel()); 
} 

更新: 你需要這個actionmethod:

public ActionResult StockRelocate() 
{ 
    var model = new RelocateStockModel(); 
    model.products = PopulateProductId(); 
    model.Fromlocations = PopulateLocation(); 
    model.Tolocations = PopulateLocation(); 
    return PartialView("_RelocateStockView", model); 
} 
+0

不錯的點@Kirill Bestemyanov但仍然發生相同的錯誤。 – 2012-07-30 09:42:51

+0

是的,現在它工作。謝謝@Kirill Bestemyanov – 2012-07-30 12:30:30

0

這意味着什麼是空的。在視圖的該部分放置一個斷點,然後將鼠標懸停在每個對象上以確定哪些爲空。

此外,很難判斷您是使用lambda還是Model對象,因爲您已經使用小寫調用了lamba變量「model」。

+0

我改變了我的lambda表達與m而不是模型。我不能把斷點放在View中,這是我首先調用的默認視圖。 – 2012-07-29 10:16:38

+0

您可以在視圖中放置斷點。右鍵單擊DropDownList行 - >斷點 - >插入斷點 – 2012-07-29 11:55:29

相關問題