获取已发布的viewmodel的空引用

时间:2014-04-01 15:33:54

标签: asp.net-mvc asp.net-mvc-4

我有这个GET

  public ActionResult Add(int productId, int categoryId, int styleId)
        {
          ...

和这篇文章:

 [HttpPost]
 public ActionResult Add(OrderDetailsViewModel thisWindowModel)
 {
    if (!ModelState.IsValid)
    {
      return View(thisWindowModel);
    }

在我的视图中,我在这里得到一个空引用异常:

  <h2>Order Details:@Model.StyleName</h2>

即使我在视图中有这个

 @Html.HiddenFor(x=>x.StyleName)

我已经调试了它,并且可以看到这是一个预期的字符串。

当我从我的视图中完全删除该行时,仍然会获得空引用异常....

有什么想法吗?它对我没有意义!

enter image description here

由于

更新

获取方法是:

   public ActionResult Add(int productId, int categoryId, int styleId)
{
AccountInfo objAccData = new AccountInfo();
AccountData thisAccInfo = objAccData.GetInfo("ABC");//TempData["FakeAccNo"].ToString());
CheckSupplier objCheckFrameSupp = new CheckSupplier(styleId, thisAccInfo.DepNo);
int FrameSupp = objCheckFrameSupp.GetFrameSupplier();


WindowColoursFromStyle objColItems = new WindowColoursFromStyle(styleId);


WindowsOptionsFromStyleSupplier objStyleOps = new WindowsOptionsFromStyleSupplier(styleId, FrameSupp, categoryId);

var repo = new ProductsRepository();
var style = repo.GetStyle(styleId);
var productCategory = repo.GetProductsCategory(categoryId);
var product = repo.GetProduct(productId);
var salesOrderId = SessionData.SalesOrderId;

return View(new OrderDetailsViewModel(salesOrderId, "Add")
{
    StyleName = style.Name,
    MinimumHeight = style.Minimum_Height,
    MinimumWidth = style.Minimum_Width,
    MaximumWidth = style.Maximum_Width,
    MaximumHeight = style.Maximum_Height,
    External_Image_Path = style.External_Image_Path,
    Category = productCategory,
    Product = product,
    InternalColours = objColItems.InternalColours,
    ExternalColours = objColItems.ExternalColours,
    CillOptions = objStyleOps.CillOptions,
    AddOnBottomOptions = objStyleOps.AddOnsOptions,
    AddOnTopOptions = objStyleOps.AddOnsOptions,
    AddOnLeftOptions = objStyleOps.AddOnsOptions,
    AddOnRightOptions = objStyleOps.AddOnsOptions,
    NoTransoms = objStyleOps.NoTransoms,
    NoDummyTransoms = objStyleOps.NoDummyTransoms,
    NoMullions = objStyleOps.NoMullions,
    NoDummyMullions = objStyleOps.NoDummyMullions,
    GlassUnits = objStyleOps.GlassUnits,
    GlassTypes = objStyleOps.GlassOptions,
    HasOpener = objStyleOps.HasOpeners,
    Handles = objStyleOps.HandleOptions,
    Lock = objStyleOps.LockOptions,
    Stay = objStyleOps.StayOptions,
    FrameThicknessBottom = objStyleOps.FrameThicknessOptions,
    FrameThicknessTop = objStyleOps.FrameThicknessOptions,
    FrameThicknessLeft = objStyleOps.FrameThicknessOptions,
    FrameThicknessRight = objStyleOps.FrameThicknessOptions,
    MullionThickness = objStyleOps.MullionThicknessOptions,
    TransomThickness = objStyleOps.TransomThinknessOptions,
    SashThinkness = objStyleOps.SashThinknessOptions,
    FrameVent = objStyleOps.VentOptions,
    SashVent = objStyleOps.VentOptions,
    WindowStyleID = styleId,
    AccountNumber ="BTCA",// TempData["FakeAccNo"].ToString(),
    DepotNumber = thisAccInfo.DepNo,
    FrameSupplier = FrameSupp,
    FrameNumber = 1,// (int)TempData["FrameNumber"],
    DripBars = objStyleOps.DripBarOptions,
    DrainageOptions = objStyleOps.DrainageOptions,
    RestrictorOptions = objStyleOps.RestrictorOptions,
    ReinforcingTypes = objStyleOps.ReinforcingTypes,
    ReinforcingRules = objStyleOps.ReinforcingRules,
    IsDoor = objStyleOps.isDoor,
        KickPlateOptions = objStyleOps.KickPlateOptions
    });
}

查看前几行是最终......

@model ViewModel.OrderDetailsViewModel



<div class="row heading-row">
    <div class="col-md-8">
        @*<h2>Order Details:@(Model.StyleName)</h2>*@
    </div>


</div>

@Html.Partial("_OrderDetailsForm",Model)

这是一个基本上加载选择/下拉列表的表单,

部分视图以:

开头
    @model ViewModel.OrderDetailsViewModel



@using (Html.BeginForm(Model.FormType, "Products", FormMethod.Post, new { @class = "form-horizontal" }))
{

1 个答案:

答案 0 :(得分:0)

试试这个:将AddDetailsViewModel like参数添加到GET动作。这是一个从POST动作发出的模型:

public ActionResult Add(int productId, int categoryId, int styleId, OrderDetailsViewModel modelComesFromPostAction)
{
  if(modelComesFromPostAction == null)
  {
    //your GET action code will get here.
  }
  else
  {
     return View(modelComesFromPostAction);
  }   
}