1

below are my View that contain a drop down list.

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

and this is my model class.

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

}
}

and this is my controller.

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

}

and when i try to run my code i got an error in my view at @Html.DropDownListFor ... it says:

Object reference not set to an instance of an object.

What does it mean?

2
  • Please show us your controller. I suspect your class isn't initialized in your controller. Make sure you have something like the following in your controller: var m = new RelocateStockModel(); return View(m); Commented Jul 29, 2012 at 23:45
  • You don't put your model in return PartialView("_RelocateStockView") Commented Jul 30, 2012 at 9:18

2 Answers 2

3

You don't show your controller, so i suppose, you don't send your model to view. Try to use this action method in your controller:

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

Update: You need this actionmethod:

public ActionResult StockRelocate()
{
    var model = new RelocateStockModel();
    model.products = PopulateProductId();
    model.Fromlocations = PopulateLocation();
    model.Tolocations = PopulateLocation();
    return PartialView("_RelocateStockView", model);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice point @Kirill Bestemyanov but still the same error is occured.
0

It means something is null. Put a breakpoint on that part of the view and then hover over each object to figure out what is null.

Also, it's hard to tell whether you're using the lambda or the Model object, because you've called your lamba variable "model" with lowercase.

2 Comments

I have changed my lambda expression with m instead of model. And i cant put breakpoint in View and this is my default view that calls first.
You can put a breakpoint in a view. Right click the DropDownList line -> Breakpoint -> Insert breakpoint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.