1

下面是我的视图,其中包含一个下拉列表。

@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......它说:

你调用的对象是空的。

这是什么意思?

4

2 回答 2

3

你不显示你的控制器,所以我想,你不发送你的模型来查看。尝试在您的控制器中使用此操作方法:

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

更新:您需要此操作方法:

public ActionResult StockRelocate()
{
    var model = new RelocateStockModel();
    model.products = PopulateProductId();
    model.Fromlocations = PopulateLocation();
    model.Tolocations = PopulateLocation();
    return PartialView("_RelocateStockView", model);
}
于 2012-07-29T21:12:05.883 回答
0

这意味着有些东西是空的。在视图的该部分放置一个断点,然后将鼠标悬停在每个对象上以找出什么是空的。

此外,很难判断您使用的是 lambda 对象还是 Model 对象,因为您使用小写字母调用了您的 Lamba 变量“model”。

于 2012-07-29T10:05:30.653 回答