Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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
Asp.net mvc 将ViewModel列表数据传递给控制器_Asp.net Mvc_Viewmodel - Fatal编程技术网

Asp.net mvc 将ViewModel列表数据传递给控制器

Asp.net mvc 将ViewModel列表数据传递给控制器,asp.net-mvc,viewmodel,Asp.net Mvc,Viewmodel,我有这样一个ViewModel: public class AddNewsModel { public List<CategoriesModel> Category { get; set; } public NewsModel NewsModel { get; set; } } 新闻模型包括: public class CategoriesModel { public string Name { get; set; } public int ID

我有这样一个ViewModel:

public class AddNewsModel
{
    public List<CategoriesModel> Category { get; set; }

    public NewsModel NewsModel { get; set; }
}
新闻模型包括:

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

    public int ID { get; set; }
}
public class NewsModel
{
    public int ID { get; set; }
    public string Category { get; set; }
    public String Headline { get; set; }
    public string Source { get; set; }
    public DateTime Publish_Date { get; set; }
    public string Text { get; set; }
    public string Summary { get; set; }

    public string TimeAgo { get; set; }

    public string ImageURL { get; set; }

    public string CategoryID { get; set; }
}
我有一个使用NewsModel为新闻获取表单输入的视图,但我想将可能的类别显示为下拉列表或从CategoriesModel中选择标记

我的看法是:

<h2>Add a News Article</h2>
@if (TempData["Success"] != null)
{
    <p class="alert alert-success" id="successMessage">@TempData["Success"]</p>
}
@using (Html.BeginForm("AddNews", "Admin", FormMethod.Post))
{

    @Html.AntiForgeryToken()
    <div class="form-horizontal" id="addNews">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.Label("News ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-1">
                @Html.EditorFor(model => model.NewsModel.ID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.ID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Category, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.DropDownListFor(model => model.Category, new SelectList(Model.Category), "Select Category")
            @*<div class="col-md-2">
                    <select form="addNews" id="NewsModel_Category" name="NewsModel.Category">
                        @foreach (var item in Model.Category)
                        {
                            <option value="@item.Name">@item.Name</option>
                        }
                    </select>
                </div>*@
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Headline, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.Headline, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Headline, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Source, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.Source, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Source, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Publish Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.NewsModel.Publish_Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Publish_Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Text, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.TextAreaFor(model => model.NewsModel.Text, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Text, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Summary, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.TextAreaFor(model => model.NewsModel.Summary, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Summary, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.ImageURL, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.ImageURL, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.ImageURL, "", 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>
}

将我的模型更改为Ienumerable而不是list会导致控制器中出现我无法解决的错误。

您的模型应如下所示:

public class AddNewsModel
{
    public IEnumerable<SelectListItem> CategorySelectList { get; set; }

    public int CategoryId {get; set;}

    public NewsModel NewsModel { get; set; }
}

有关更多详细信息->

您应该拥有模型中所有类别的目录和列表

public class AddNewsModel
{
    public int Category { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set;}
    public NewsModel NewsModel { get; set; }
}
公共类AddNewsModel
{
公共int类{get;set;}
公共IEnumerable类别{get;set;}
公共新闻模型新闻模型{get;set;}
}
看法

使用列表更新您的下拉列表

<h2>Add a News Article</h2>
@if (TempData["Success"] != null)
{
    <p class="alert alert-success" id="successMessage">@TempData["Success"]</p>
}
@using (Html.BeginForm("AddNews", "Admin", FormMethod.Post))
{

    @Html.AntiForgeryToken()
    <div class="form-horizontal" id="addNews">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.Label("News ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-1">
                @Html.EditorFor(model => model.NewsModel.ID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.ID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Category, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.DropDownListFor(model => model.Category, Model.Categories, "Select Category")
            @*<div class="col-md-2">
                    <select form="addNews" id="NewsModel_Category" name="NewsModel.Category">
                        @foreach (var item in Model.Category)
                        {
                            <option value="@item.Name">@item.Name</option>
                        }
                    </select>
                </div>*@
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Headline, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.Headline, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Headline, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Source, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.Source, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Source, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Publish Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.NewsModel.Publish_Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Publish_Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Text, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.TextAreaFor(model => model.NewsModel.Text, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Text, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Summary, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.TextAreaFor(model => model.NewsModel.Summary, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Summary, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.ImageURL, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.ImageURL, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.ImageURL, "", 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>
}
添加新闻文章
@if(TempData[“Success”]!=null)
{

@TempData[“成功”]

} @使用(Html.BeginForm(“AddNews”、“Admin”、FormMethod.Post)) { @Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Label(“News ID”,htmlAttributes:new{@class=“control Label col-md-2”}) @EditorFor(model=>model.NewsModel.ID,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.NewsModel.ID,“,new{@class=“text danger”}) @LabelFor(model=>model.NewsModel.Category,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownListFor(model=>model.Category,model.Categories,“选择类别”) @* @foreach(Model.Category中的var项) { @项目名称 } *@ @LabelFor(model=>model.NewsModel.Headline,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.NewsModel.Headline,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.NewsModel.Headline,“,new{@class=“text danger”}) @LabelFor(model=>model.NewsModel.Source,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.NewsModel.Source,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.NewsModel.Source,“,new{@class=“text danger”}) @Label(“发布日期”,htmlAttributes:new{@class=“control Label col-md-2”}) @EditorFor(model=>model.NewsModel.Publish_Date,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.NewsModel.Publish_Date,“,new{@class=“text danger”}) @LabelFor(model=>model.NewsModel.Text,htmlAttributes:new{@class=“controllabel col-md-2”}) @Text区域(model=>model.NewsModel.Text,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.NewsModel.Text,“,new{@class=“Text danger”}) @LabelFor(model=>model.NewsModel.Summary,htmlAttributes:new{@class=“controllabel col-md-2”}) @text区域(model=>model.NewsModel.Summary,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.NewsModel.Summary,“,new{@class=“text danger”}) @LabelFor(model=>model.NewsModel.ImageURL,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.NewsModel.ImageURL,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.NewsModel.ImageURL,“,new{@class=“text danger”}) }
新建选择列表(Model.Category,“Id”,“Name”)
(但视图模型应包含属性
IEnumerable CategoryList
,而不是
列表
)。并且您不能对绑定到的属性和SelectList使用相同的名称。它需要是
@Html.DropDownListFor(m=>m.NewsModel.Category,…)
我在控制器操作中显式强制转换后,尝试了这个方法以及@Irakli的答案,它可以工作如果我在控制器中填充类别,我必须在下面的代码中进行哪些更改<代码>公共操作结果AddNews(){AddNewsModel AddNewsModel=new AddNewsModel();AddNewsModel.Categories=new NewsArticles().GetCategories();返回视图(AddNewsModel);}AddNewsModel.CategoritySelectList=new NewsArticles().GetCategories().Select(x=>new SelectListItem()){Text=x.Name,Value=x.ID.ToString();})我认为它应该可以工作“@Html.DropDownListFor(model=>model.Category,IEnumerable Categories,“Select Categories”)”在视图中抛出错误,指出“使用泛型IEnumerable需要1个类型参数”,“SelectListItem”是在给定上下文中无效的类型”,“名称类别在当前上下文中不存在。”您的代码甚至未编译!如果编译,则会引发异常。DownVorters..请留下评论。这有助于改进答案!!
@Html.DropDownListFor(model => model.CategoryId, Model.CategorySelectList, "Select Category")
public class AddNewsModel
{
    public int Category { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set;}
    public NewsModel NewsModel { get; set; }
}
<h2>Add a News Article</h2>
@if (TempData["Success"] != null)
{
    <p class="alert alert-success" id="successMessage">@TempData["Success"]</p>
}
@using (Html.BeginForm("AddNews", "Admin", FormMethod.Post))
{

    @Html.AntiForgeryToken()
    <div class="form-horizontal" id="addNews">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.Label("News ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-1">
                @Html.EditorFor(model => model.NewsModel.ID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.ID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Category, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.DropDownListFor(model => model.Category, Model.Categories, "Select Category")
            @*<div class="col-md-2">
                    <select form="addNews" id="NewsModel_Category" name="NewsModel.Category">
                        @foreach (var item in Model.Category)
                        {
                            <option value="@item.Name">@item.Name</option>
                        }
                    </select>
                </div>*@
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Headline, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.Headline, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Headline, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Source, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.Source, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Source, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Publish Date", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.NewsModel.Publish_Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Publish_Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Text, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.TextAreaFor(model => model.NewsModel.Text, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Text, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.Summary, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.TextAreaFor(model => model.NewsModel.Summary, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.Summary, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NewsModel.ImageURL, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-6">
                @Html.EditorFor(model => model.NewsModel.ImageURL, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NewsModel.ImageURL, "", 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>
}