[英]In an MVC4 controller action, how can I validate a model only when it's passed in?
[英]Following the MVC4 tutorial, where does “Model” come from and how can I change it's value?
VisualStudio2010告訴我它是Model Property associated with System.Mvc.ViewDataDictionary Object
的Model Property associated with System.Mvc.ViewDataDictionary Object
這是有幫助的,但DataDictionary的文檔似乎並沒有以易於理解的格式向我展示我想要的內容,至少。
請注意,以下.cshtml
文件位於BorkedModel
的View文件夾中,但我想訪問的模型位於ExampleModel
的Model文件夾中。 如果文件本身位於ExampleModel
的View文件夾中,而不是在不同模型的View文件夾中,則此確切代碼將起作用。
ProjectTopDirectory / Views / BorkedModel /index.cshtml
@model IEnumerable<ExampleMVC4.Models.ExampleModel> @foreach (var item in Model) { <HTML here/> }
是否有解決方案我正在做什么,或者我應該只是將模型保存在他們自己的視圖中,那就是它? 有沒有辦法從另一個模型的視圖訪問一個模型?
要在視圖中使用強類型模型,您需要將模型傳遞給控制器的視圖:
public ActionResult Index()
{
YourDBContext db = new YourDBContext();
IEnumerable<ExampleModel> model = db.GetDataFromDatabase();
return View(model); // Pass the model to the view
}
然后在您的視圖中指定模型,該模型應允許您訪問數據:
@model IEnumerable<NextFlicksMVC4.Models.ExampleModel>
@foreach (var item in Model)
{
// Your HTML
}
更新:您的控制器操作方法應該從數據庫中檢索數據,然后將其傳遞給視圖。 視圖的唯一作用是向用戶顯示數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.