Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC 5中为另一个实体创建或呈现实体列表?_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 如何在ASP.NET MVC 5中为另一个实体创建或呈现实体列表?

C# 如何在ASP.NET MVC 5中为另一个实体创建或呈现实体列表?,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,如何为应用程序中具有此模型的位置呈现(脚手架)标记列表: 我使用的是实体框架6.1 我想保存一个位置的标签列表,并将其渲染回视图,但我无法做到这一点 public class Tag { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public virtual int Id { get; set; } public virtual string Na

如何为应用程序中具有此模型的位置呈现(脚手架)标记列表:

我使用的是实体框架6.1

我想保存一个位置的标签列表,并将其渲染回视图,但我无法做到这一点

public class Tag
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }

        // Navigation
        public virtual Location Location { get; set; }

    }

    public class Location
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual int LocationId { get; set; }
        public virtual string LocationName { get; set; }

        // Navigation
        public virtual List<Tag> Tags { get; set; }

    }
公共类标记
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
//航行
公共虚拟位置{get;set;}
}
公共类位置
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共虚拟int LocationId{get;set;}
公共虚拟字符串位置名称{get;set;}
//航行
公共虚拟列表标记{get;set;}
}
现在视图只包含输入的位置名称:

查看-创建位置

<h2>Create</h2>
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

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

        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Tags, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Tags, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Tags, "", 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>
}
创建
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
位置

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.LocationName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.LocationName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.LocationName,“,new{@class=“text danger”}) @LabelFor(model=>model.Tags,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.TextBoxFor(model=>model.Tags,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Tags,“,new{@class=“text danger”}) }
它呈现良好,但不会在数据库中保存任何标记:

问题出在这里

当我试图在“编辑”视图中渲染回标记时,它显示如下:


谢谢你的帮助

您要查找的是“标记”属性的自定义查看/编辑模板。请参阅:

您的问题是什么?@BenRobinson感谢您的回复,我想创建/保存一个与特定位置相关的标签列表。你能帮个忙吗?亲爱的投票人,为什么要投票?更具体地说,你不能做的是什么,你在做你需要的事情时遇到了什么问题?谢谢你的大卫,但我不明白在我的场景中我是如何做的…请猜一下?