MVC模型出错

时间:2015-01-13 09:16:13

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

我在尝试加载标签时遇到错误。

错误

  

传递到字典中的模型项是类型的   'Mvc5.Models.IndexModel',但是这个字典需要一个模型项   输入'Mvc5.Models.ORDERMetadata'。

订单控制器

  public ActionResult order()
    {
       IndexModel models = new IndexModel();

        return View(models);
    }

IndexModel.cs

        public class IndexModel
        {
            public ORDERMetadata GeneralTab { get; set; }
            public ORDER_DETAILSMetadata ItemTab { get; set; }
            //public ThirdTabModel ThirdTab { get; set; }

        }

order.cshtml

@model Mvc5.Models.IndexModel

<div id="tabs">
    <ul class="nav nav-tabs">
        <li><a href="#tabs-1">General</a></li>
        <li><a href="#tabs-2">Item</a></li>
        <li><a href="#tabs-3">Total</a></li>
    </ul>


    <div id="tabs-1">
        @{Html.RenderPartial("_Partial_General_Tab", Model.GeneralTab);}  <===== ERRROR HERE!
    </div>

    <div id="tabs-2">
        @{Html.RenderPartial("_Partial_Item_Tab", Model.ItemTab);}
    </div>

    <div id="tabs-3">
        Content for Tab 3 goes here.<br />
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
        sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
    </div>

</div>

_partial_General_Tab.cshtml

@model Mvc5.Models.ORDERMetadata

    @using (Ajax.BeginForm("Edit", "Order",
            new AjaxOptions
            {
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "target"
            }))
    {

        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        Enter model data here....   

        <button id="editorder" type="submit" class="btn btn-default">Save</button>



    }

_Partial_Item_Tab.cshtml

@model Mvc5.Models.ORDER_DETAILSMetadata


    @using (Ajax.BeginForm("Items", "Order",
            new AjaxOptions
            {
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "target"
            }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        Enter model data here....   

        <input type="submit" value="Add" class="btn btn-default" />

    }

1 个答案:

答案 0 :(得分:0)

这将会发生,因为GeneralTab的{​​{1}}和/或ItemTab属性为IndexModel(在这种情况下,传递给部分的null将会是ViewDataDictionary。确保在将模型传递给视图之前初始化属性。

IndexModel

或者在public ActionResult order() { IndexModel models = new IndexModel(); models.GeneralTab = new ORDERMetadata(); models.ItemTab = new ORDER_DETAILSMetadata(); return View(models); }

的无参数构造函数中初始化它们