MVC 模型出现错误
MVC model getting error
我在尝试加载选项卡时遇到错误。
错误
The model item passed into the dictionary is of type
'Mvc5.Models.IndexModel', but this dictionary requires a model item of
type '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" />
}
这将发生,因为 IndexModel
的 GeneralTab
and/or ItemTab
属性是 null
(在这种情况下 ViewDataDictionary
通过部分将是IndexModel
。确保在将模型传递给视图之前初始化属性。
public ActionResult order()
{
IndexModel models = new IndexModel();
models.GeneralTab = new ORDERMetadata();
models.ItemTab = new ORDER_DETAILSMetadata();
return View(models);
}
或者在 IndexModel
的无参数构造函数中初始化它们
我在尝试加载选项卡时遇到错误。
错误
The model item passed into the dictionary is of type 'Mvc5.Models.IndexModel', but this dictionary requires a model item of type '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" />
}
这将发生,因为 IndexModel
的 GeneralTab
and/or ItemTab
属性是 null
(在这种情况下 ViewDataDictionary
通过部分将是IndexModel
。确保在将模型传递给视图之前初始化属性。
public ActionResult order()
{
IndexModel models = new IndexModel();
models.GeneralTab = new ORDERMetadata();
models.ItemTab = new ORDER_DETAILSMetadata();
return View(models);
}
或者在 IndexModel