目前我正在使用asp mvc視圖 我想要在一個視圖上呈現數據......它們在同一個視圖上顯示數據並創建表單,但它不能完成......因爲控制器返回IEnumerable類型的數據並創建表單的editorfor不包含對此的定義。想要在同一個視圖上使用同一個模型兩次不同的類型?
我有一個模型"T4.Models.Order"
,如果我用這個模型作爲@model IEnumerable<T4.Models.Order>
創建表單剃刀語法顯示錯誤
,如果我用這個模型作爲@model T4.Models.Order
顯示數據顯示的錯誤
這裏是我的代碼
@model IEnumerable<T4.Models.Order>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CustomerID)
</th>
<th>
@Html.DisplayNameFor(model => model.CustomerID)
</th>
<th>
@Html.DisplayNameFor(model => model.OrderDate)
</th>
<th>
@Html.DisplayNameFor(model => model.RequiredDate)
</th>
<th>
@Html.DisplayNameFor(model => model.ShippedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipVia)
</th>
<th>
@Html.DisplayNameFor(model => model.Freight)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipName)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipAddress)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipCity)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipRegion)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipPostalCode)
</th>
<th>
@Html.DisplayNameFor(model => model.ShipCountry)
</th>
</tr>
</thead>
@*@{
IEnumerable<T4.Models.Order> m = @model;
}*@
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerID)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.RequiredDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShippedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipVia)
</td>
<td>
@Html.DisplayFor(modelItem => item.Freight)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipCity)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipRegion)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipPostalCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShipCountry)
</td>
</tr>
}
</table>
</body>
</html>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Order</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.CustomerID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerID)
@Html.ValidationMessageFor(model => model.CustomerID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmployeeID, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmployeeID)
@Html.ValidationMessageFor(model => model.EmployeeID)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderDate)
@Html.ValidationMessageFor(model => model.OrderDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RequiredDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RequiredDate)
@Html.ValidationMessageFor(model => model.RequiredDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShippedDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShippedDate)
@Html.ValidationMessageFor(model => model.ShippedDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipVia, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipVia)
@Html.ValidationMessageFor(model => model.ShipVia)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Freight, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Freight)
@Html.ValidationMessageFor(model => model.Freight)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipName)
@Html.ValidationMessageFor(model => model.ShipName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipAddress, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipAddress)
@Html.ValidationMessageFor(model => model.ShipAddress)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipCity, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipCity)
@Html.ValidationMessageFor(model => model.ShipCity)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipRegion, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipRegion)
@Html.ValidationMessageFor(model => model.ShipRegion)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipPostalCode, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipPostalCode)
@Html.ValidationMessageFor(model => model.ShipPostalCode)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipCountry, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipCountry)
@Html.ValidationMessageFor(model => model.ShipCountry)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
控制器代碼:
public ActionResult Index()
{
return View(db.Order.ToList());
}
注:我不想改變控制器側什麼。
你混的代碼顯示,並在控制器的一個操作方法創建表單?我認爲你應該使用局部視圖,用'@Html調用它。ActionLink',並在控制器中單獨創建並顯示Actions。 – stenlytw
^哎呀對不起,我的意思是@ Html.Action而不是'@ Html.ActionLink' – stenlytw
我不想在控制器端使用任何partialview或任何改變..我必須在cshtml上使用razor語法..也我不能使用html ...請提供我的解決方案,如果你有...謝謝 –