【发布时间】:2016-08-17 22:19:42
【问题描述】:
目前我正在使用 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());
}
注意:我不想更改控制器端的任何内容。
【问题讨论】:
-
你是否在控制器的一个Action方法中混合显示和创建表单的代码?我认为你应该使用局部视图,用
@Html.ActionLink调用它,并在控制器中单独创建和显示操作。 -
^抱歉,我的意思是
@Html.Action,而不是@Html.ActionLink -
我不想使用任何局部视图或控制器端的任何更改..我必须在 cshtml 上使用剃刀语法..所以我也不能使用 html...如果你请给我解决方案有...谢谢你
-
请提供您的控制器代码。这取决于您的代码现在的外观。 stackoverflow.com/a/13852500/3387187
-
@bounces 您提供的链接也有部分视图解决方案..正如我提到的我不想使用任何部分视图..正如你提到的我也更新了我的控制器端代码..请看那个代码...
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor ienumerable