简体   繁体   English

如何在剃刀视图中使用数据库视图?

[英]How can I use a view of my database in a razor view?

I have the following code but I can not auto-fill the options of a select: 我有以下代码,但无法自动填写选择的选项:

controller.cs controller.cs

public ActionResult GetPais()
        {    
            using (MyEntities ctx = new MyEntities())
            {
                var List = ctx.Countries.ToList();
                return PartialView("_optionsPais", List);
            }
        }

modelCountriesList.cs modelCountriesList.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace MySite.Models
    {
        public class CountrisList
        {
            public IEnumerable<Countries> Countries { get; set; }
        }
    }

MySite.Context.cs MySite.Context.cs

namespace MySite.Models
    {
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class MyEntities : DbContext
    {
        public MyEntities()
            : base("name=MyEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
        ...
        public DbSet<Countries> Countries { get; set; }
        ...
        }
    }

_optionPais.cshtml _optionPais.cshtml

@model MySite.Models.CountrisList
@using System.Web.Configuration;
@using System.Globalization;
@using System.Linq;


@foreach (var item in Model.Countries)
{
    <option value="@item.Ide">@item.Descripcion</option>
}

I just want to iterate all the rows of my SQL table: 我只想迭代SQL表的所有行:

我的SQL表

Site.edmx

I could not find the way to iterate the rows of my table, they could help me. 我找不到迭代表中行的方法,它们可以为我提供帮助。

or Find another type of path like using LINQ directly in the view, thanks 或查找其他类型的路径,例如直接在视图中使用LINQ,谢谢

You should be getting an error while trying access the Countries property of a List<Countries> object in your Razor view. 你应该试图访问会得到一个错误Countries一个财产List<Countries>对象在Razor视图。

Instead, instantiate the view model in the controller action and pass it to PartialView() since your Razor view defines its model as MySite.Models.CountrisList . 相反,由于您的Razor视图将其模型定义为MySite.Models.CountrisList ,因此在控制器操作中实例化视图模型并将其传递给PartialView()

public ActionResult GetPais()
{    
    using (MyEntities ctx = new MyEntities())
    {
        var model = new MySite.Models.CountrisList();
        model.Countries = ctx.Countries.ToList();
        return PartialView("_optionsPais", model);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我的表不可枚举时,如何显示 SQL 数据库中的列表? 我可以很好地使用详细信息视图,但不能使用索引视图 - How do I display a list from SQL database when my table is not enumerable? I can use details view fine, but not an index view 如何在我的 ASP.NET Core Razor 视图中更新/刷新 ViewData? - How can I update/refresh ViewData in my ASP.NET Core Razor View? Asp .net Core:如何在Razor Pages的Partial View中使用模型? - Asp .net Core : How can I use a model inside a Partial View in Razor Pages? 如何使用从剃刀视图插入到视图包中的 linq 连接函数结果? - How can i use the linq join function result inserted in a viewbag from razor view? 如何在Razor视图中使用选定值(而非模型值)作为后期参数? - How can I use selected value (not model value) as Post Parameter In Razor View? 如何从 byte[] 加载程序集以在 ASP.NET Core 的 Razor 视图中使用? - How can I load an assembly from a byte[] for use in a Razor view in ASP.NET Core? 我如何在Razor的部分视图中获取HTMLPREFIX - How can I get HTMLPREFIX inside the partial view i Razor 我如何发现 CSS 在 Razor 视图(模型)中不起作用的原因? - How can I spot why the CSS not working in Razor View (model)? 如何在带有下拉菜单的Razor视图中过滤数据 - How can I filter data in a Razor view with a dropdown 如何从模型绑定Collection以查看MVC Razor - How can I bind Collection from model to view MVC Razor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM