2016-04-24 94 views
0

目前我正在使用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()); 
} 

注:我不想改變控制器側什麼。

+0

你混的代碼顯示,並在控制器的一個操作方法創建表單?我認爲你應該使用局部視圖,用'@Html調用它。ActionLink',並在控制器中單獨創建並顯示Actions。 – stenlytw

+0

^哎呀對不起,我的意思是@ Html.Action而不是'@ Html.ActionLink' – stenlytw

+0

我不想在控制器端使用任何partialview或任何改變..我必須在cshtml上使用razor語法..也我不能使用html ...請提供我的解決方案,如果你有...謝謝 –

回答

0

只要創建模型類的對象,用於呈現您的表格..

這裏是代碼

@{ 
    T4.Models.Order o = new T4.Models.Order(); 
} 

表單

@using (Html.BeginForm("Create","Orders")) 
    { 
     @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Order</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => o.CustomerID, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.CustomerID, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.CustomerID, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.EmployeeID, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.EmployeeID, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.EmployeeID, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.OrderDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.OrderDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.OrderDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.RequiredDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.RequiredDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.RequiredDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShippedDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShippedDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShippedDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipVia, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipVia, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipVia, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.Freight, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.Freight, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.Freight, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipAddress, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipAddress, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipAddress, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipCity, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipCity, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipCity, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipRegion, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipRegion, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipRegion, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipPostalCode, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipPostalCode, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipPostalCode, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => o.ShipCountry, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => o.ShipCountry, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => o.ShipCountry, "", new { @class = "text-danger" }) 
      </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> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 
0

如果要列出現有的訂單,並顯示在同一個視圖中創建表單,您應該創建具有這種屬性的新視圖模型。您可以從您現有的T4.Models.Order繼承此項。現在

public class CreateAndListVm : T4.Models.Order 
{ 
    public List<Order> Orders { set;get;} 
    public CreateAndListVm() 
    { 
    this.Orders = new List<Order>(); 
    } 
} 

你的觀點會被強類型到這個視圖模型

@model CreateAndListVm 
<h2>New item</h2> 
@using(Html.BeginForm()) 
{ 
    @Html.TextBoxFor(s=>s.CustomerID) 
    <input type="submit" /> 
} 
<h3>Existing orders</h3> 
@foreach(var item in Model.Orders) 
{ 
    <p>@item.CustomerID</p> 
} 

Ofcourse意思,你應該從你的操作方法發送的CreateAndListVm的對象。

public ActionResult Index() 
{ 
    var vm = new CreateAndListVm(); 
    // to do : Load the orders to vm.Orders; 
    return View(vm) 
} 

如果您不想更改控制器並將Order的集合返回給視圖,則可以使用另一種方法。不要使用HTML輔助方法來生成輸入表單字段。只要寫的HTML代碼輸入字段自己。

假設你的操作方法將返回Order類的列表

public ActionResult Index() 
{ 
    var orders = new List<Order>(); 
    return View(orders); 
} 

現在,在您看來這是強類型,以訂單的集合

@model IEnumerable<Order> 
<h2>Create</h2> 
<form action="Create" method="POST"> 
    <input type="text" name="CustomerID" /> 
    <input type="submit" /> 
</form> 
<h3>Existing orders</h3> 
@foreach(var item in Model) 
{ 
    <p>@item.CustomerID</p> 
} 
+0

我不想在控制器端改變任何東西,正如我在附註中提到的那樣。請僅在視圖方面提供我的解決方案。我不想使用任何局部視圖。我可以使用「@model IEnumerable 」和「@model T4.Models.Order」兩者或不同的wa yon cshtml。 –

+0

檢查答案的第二部分。 – Shyju

+0

謝謝你的回答@shyju ..你說,沒有使用Htmlhelper,但我必須使用Htmlhelper ...請提供另一種解決方案,如果你有。 –

相關問題