如何公开从MVC模式项目的集合在ASP.NET MVC一个MVC视图(* .cshtml) [英] How to expose a collection of items from MVC model to a MVC view (*.cshtml) in ASP.NET MVC

查看:95
本文介绍了如何公开从MVC模式项目的集合在ASP.NET MVC一个MVC视图(* .cshtml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象的集合的MVC模式oject。我想揭露MVC模型作为一个MVC视图用户输入的字段。

下面是MVC模型和领域模型

 公共类BookModel
    {
        公共BookModel(书书)
        {
            this.Authors = book.Authors;
        }        公开名单<作者>作者{搞定;组; }
    }    公共类图书
    {
        公开名单<作者>作者=新的List<作者>();
    }    公共类作者
    {
        公共字符串名称{;组; }
    }

下面是控制器的操作方法:

 公众的ActionResult编辑(INT ID)
        {            BookModel模型= GetBookModel(ID);
            返回查看(模型);
        }

的问题是,所生成的视图(* .cshtml)不具有用于收集作者输入字段

任何想法将非常AP preciated。


解决方案

  

的问题是,所生成的视图(* .cshtml)不具有
  输入域集合作者。


在ASP.NET MVC新View向导会尽力。从那里,它取决于你。

所以,在 Edit.cshtml 你可以使用编辑器模板:

  @model BookModel@using(Html.BeginForm())
{
    <表>
        <&THEAD GT;
            &所述; TR>
                <第i个姓名和LT; /第i
                百分位>文字< /第i
            < / TR>
        < / THEAD>
        <&TBODY GT;
            @ Html.EditorFor(X => x.Authors)
        < / TBODY>
    < /表>    <输入类型=提交值=编辑/>
}

,然后定义为一个作者的编辑模板(〜/查看/共享/ EditorTemplates / Author.cshtml ),这将自动为您收集的每个元素被渲染:

  @model作者
&所述; TR>
    < TD> @ Html.EditorFor(X => x.Name)LT; / TD>
    < TD> @ Html.EditorFor(X => x.Text)LT; / TD>
< / TR>

I have a MVC model oject that contains a collection of object. I want to expose the MVC model as user input fields on a MVC view.

Below is MVC model and domain model

 public class BookModel
    {
        public BookModel(Book book)
        {
            this.Authors = book.Authors;
        }   

        public List<Author> Authors { get; set; }
    }

    public  class Book
    {
        public List<Author> Authors = new List<Author>();
    }

    public class Author
    {
        public string Name { get; set; }
    }

Below is the Action method in controller:

public ActionResult Edit(int id)
        {

            BookModel model = GetBookModel(id);
            return View(model);
        }

The problem is that, the generated view (*.cshtml) does NOT have the input fields for the collection authors.

Any idea would be very much appreciated.

解决方案

The problem is that, the generated view (*.cshtml) does NOT have the input fields for the collection authors.

The ASP.NET MVC new View wizard does its best. From there its up to you.

So inside Edit.cshtml you could use editor templates:

@model BookModel

@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Text</th>
            </tr>
        </thead>
        <tbody>
            @Html.EditorFor(x => x.Authors)
        </tbody>
    </table>

    <input type="submit" value="Edit" />
}

and then define a editor template for an Author (~/Views/Shared/EditorTemplates/Author.cshtml) which will be rendered automatically for each element of your collection:

@model Author
<tr>
    <td>@Html.EditorFor(x => x.Name)</td>
    <td>@Html.EditorFor(x => x.Text)</td>
</tr>

这篇关于如何公开从MVC模式项目的集合在ASP.NET MVC一个MVC视图(* .cshtml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆