@using System.Data;
@model DataSet;
@using WebApplication4.Models;
我正在使用資料集來檢索表資料,但除此之外,我還想使用另一個模型,即,@model MyViewModel如下所示:
@using System.Data;
@model DataSet
@using WebApplication4.Models;
@model MyViewModel;
我收到一個錯誤
每個檔案只出現一個模型指令
如何將這些資料集放入模式中以便僅匯入MyViewModel其中包含資料集資料的模型?
uj5u.com熱心網友回復:
一個視圖只能有一個模型。但該模型可以是您喜歡的任何東西。
例如,如果您希望模型具有DataSet屬性:
public DataSet MyDataSet { get; set; }
或者你可能想創建一個由 aDataSet和你的模型組成的自定義視圖模型:
public class MyCompositeViewModel
{
public DataSet MyDataSet { get; set; }
public MyViewModel ViewModel { get; set; }
}
你可以構造任何你喜歡從控制器傳遞到視圖的物件。因此,雖然您只能使用單個模型實體,但該模型實體可以由視圖所需的任何內容組成。
uj5u.com熱心網友回復:
你將不得不創建一個特殊的視圖模型類
public class ViewModel
{
public DataSet MyDataSet { get; set; }
public MyAnotherModel MyAnotherModel { get; set; }
}
獲取視圖操作
[HttpGet]
public IActionResult GetMyView()
{
var dataSet = ... your code
myAnotherModel = ... your coe
var model= new ViewModel
{
MyDataSet =dataSet;
MyAnotherModel =myAnotherModel
}
return View (model);
}
獲取視圖
@model ViewModel
....
POST 視圖操作
[HttpPost]
public IActionResult PostMyView(ViewModel model)
{
.... your code
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397516.html
標籤:C# asp.net核心 asp.net-core-mvc ado.net .net-6.0
