【问题标题】:How to post a list from view to controller mapped to viewModel in Asp.net core?如何将列表从视图发布到映射到 Asp.net 核心中 viewModel 的控制器?
【发布时间】:2018-02-14 16:54:38
【问题描述】:

这是我的视图模型

public class VideoViewModel
{
    public string Movie { get; set; }
    public int Order { get; set; }

    public IList<VideoContent> VideoContents { get; set; }
}

如何将数据从视图中的字段传递到映射到我的 viewModel 中的集合的控制器?

在我看来,这段代码正在将 Movie 和 Order 传递给控制器​​,但我无法确定 hos 也发送集合。

@model ViewModels.VideoViewModel
    <form method="post">
        <div class="form-group">
            <label for="movie">Movie</label>
            <input type="text" name="Movie" id="movie" class="form-control">
        </div>
        <div class="form-group">
            <label for="order">Order</label>
            <input type="text" name="Order" id="order" class="form-control">
        </div>            
        <button type="submit" class="btn btn-primary">Add to database</button>
    </form>

提前致谢!

/卡勒

【问题讨论】:

    标签: asp.net asp.net-core controller viewmodel


    【解决方案1】:

    ASP .NET Core 使用 name 属性将输入字段中的数据绑定到视图模型。

    这应该适用于传递集合。

    for(int c = 0; c < VideoContents.Count; c++)
    {      
        <input name="VideoContents[@c].FirstProperty" value="@Model.VideoContents[@c].FirstProperty"/>
        <input name="VideoContents[@c].SecondProperty" value="@Model.VideoContents[c].SecondProperty"/>
    }
    

    【讨论】:

    • 非常感谢!你拯救了我的一天:-)
    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多