C# 在MVC中从视图访问和修改列表

C# 在MVC中从视图访问和修改列表,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我正在编写一个程序,允许用户创建食谱。在其他属性中,配方将与许多步骤/说明相关联。我的观点如下: @model Recipe.Models.Input @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <style> .editor-field textarea { width: 400px; height: 100px; } </style> <h2

我正在编写一个程序,允许用户创建食谱。在其他属性中,配方将与许多步骤/说明相关联。我的观点如下:

@model Recipe.Models.Input

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<style>
.editor-field textarea {
    width: 400px;
    height: 100px;
}
</style>

<h2>New Recipe</h2>

<div class="col-md-4">
<h3>General Information</h3>
<div>
    @Html.LabelFor(m => m.name)<br />
    @Html.EditorFor(m => m.name)<br />
</div>
<div class="editor-field">
    @Html.LabelFor(m => m.description)<br />
    @Html.TextAreaFor(m => m.description, "Recipe Description")
</div>
<div>
    @Html.LabelFor(m => m.prepCookTime)
    @Html.EditorFor(m => m.prepCookTime)
</div>
<div>
    @Html.LabelFor(m => m.cookTime)<br />
    @Html.EditorFor(m => m.cookTime)
</div>
</div>
<div class="col-md-4">
<h3>Nutritional Information (?)</h3>
</div>
<div class="col-md-4">
@using (Html.BeginForm("AddStep", "Input"))
{
    <h3>Steps</h3>
    <!-- functionality for adding an indefinate amount of steps -->

    <div>
        <div>
            @Html.LabelFor(m => m.step)<br />
            @Html.EditorFor(m => m.step)<br />
        </div>
        <div class="editor-field">
            @Html.LabelFor(m => m.stepDescription)<br />
            @Html.TextAreaFor(m => m.stepDescription)<br />
        </div>
        <br /><input type="submit" value="Add Step" class="btn btn-default" />
    </div>
    <div>
        <!-- functionality for displaying added steps -->

        <h6>Number of steps: @Model.Steps.Count</h6> <!-- This is not changing -->

    </div>
}
(我为奇怪的格式表示歉意,它复制得很奇怪。) 我发现,在这种设置下,我从视图本身访问列表的权限非常有限。我可以向列表中添加一个元素,但似乎只要AddStep操作方法返回,它就会被丢弃。无论我添加了多少项,每次运行它时,视图都会打印出“步骤数:1”。我是否遗漏了什么,或者这是一种不正确的访问列表的方式

提前谢谢

编辑:我想模型可能很重要

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

namespace Recipe.Models
{

public class Input
{
    /* Properties */
    [Display(Name = "Name")]
    public string name { get; set; } //represent the name of the recipe
    //public file? imageField
    [Display(Name = "Store Number")]
    public int store { get; set; } //represent store number
    [Display(Name = "Description")]
    public string description { get; set; } //represent recipe description
    [Display(Name = "Estimated Preparation Time")]
    public DateTime prepCookTime { get; set; } //represent preparation time
    [Display(Name = "Estimated Cook Time")]
    public DateTime cookTime { get; set; } //represent cooking time

    [Display(Name = "Serving Size")]
    public int servingSize { get; set; }
    [Display(Name = "Number of Servings")]
    public int servingNumber { get; set; }
    [Display(Name = "Unit of Measurement")]
    public string unitOfMeasurement { get; set; }

    public List<string> Steps { get; set; } //list to hold all steps associated with recipe
    public List<string> StepDescriptions { get; set; } //list to hold all step descriptions
    [Display(Name = "Step title/overview")]
    public string step { get; set; } //string to hold step title
    [Display(Name = "Step Description")]
    public string stepDescription { get; set; } //string to hold step description

    public Input()
    {
        //initialize lists
        Steps = new List<string>();
        StepDescriptions = new List<string>();
    }
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
名称空间配方.模型
{
公共类输入
{
/*性质*/
[显示(Name=“Name”)]
公共字符串名称{get;set;}//表示配方的名称
//公共文件?图像字段
[显示(Name=“门店编号”)]
公共整数存储{get;set;}//表示存储编号
[显示(Name=“Description”)]
公共字符串描述{get;set;}//表示配方描述
[显示(Name=“预计准备时间”)]
public DateTime prepCookTime{get;set;}//表示准备时间
[显示(Name=“预计烹饪时间”)]
公共日期时间烹调时间{get;set;}//表示烹调时间
[显示(Name=“服务大小”)]
公共int服务大小{get;set;}
[显示(名称=“份数”)]
公共int服务编号{get;set;}
[显示(名称=“计量单位”)]
公共字符串度量单位{get;set;}
公共列表步骤{get;set;}//List保存与配方关联的所有步骤
公共列表步骤说明{get;set;}//用于保存所有步骤说明的列表
[显示(Name=“步骤标题/概述”)]
公共字符串步骤{get;set;}//用于保存步骤标题的字符串
[显示(名称=“步骤说明”)]
公共字符串stepDescription{get;set;}//用于保存步骤描述的字符串
公共投入()
{
//初始化列表
步骤=新列表();
StepDescriptions=新列表();
}
}
}

由于每个请求都是无状态的,因此需要发布整个步骤列表,然后在视图中重新显示


我不确定输入类是什么,但理想情况下,它将包含一个步骤列表,作为视图的模型。然后将其发布到AddStep方法中,添加新步骤,然后将列表重新提交到视图。

web是无状态的。每次发出请求时,都会创建控制器的新实例,从而创建
输入的新实例-您需要保存数据(例如,保存到数据库)。为什么所有这些输入都是由
EditorFor()
在表单之外生成的呢?@StephenMuecke我感觉到有这样的事情发生了;那很有帮助,谢谢。为了回答您的问题,我写了BeginForm行,以确保输入中的列表被访问。不过我现在要改变这一点。谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Recipe.Models
{

public class Input
{
    /* Properties */
    [Display(Name = "Name")]
    public string name { get; set; } //represent the name of the recipe
    //public file? imageField
    [Display(Name = "Store Number")]
    public int store { get; set; } //represent store number
    [Display(Name = "Description")]
    public string description { get; set; } //represent recipe description
    [Display(Name = "Estimated Preparation Time")]
    public DateTime prepCookTime { get; set; } //represent preparation time
    [Display(Name = "Estimated Cook Time")]
    public DateTime cookTime { get; set; } //represent cooking time

    [Display(Name = "Serving Size")]
    public int servingSize { get; set; }
    [Display(Name = "Number of Servings")]
    public int servingNumber { get; set; }
    [Display(Name = "Unit of Measurement")]
    public string unitOfMeasurement { get; set; }

    public List<string> Steps { get; set; } //list to hold all steps associated with recipe
    public List<string> StepDescriptions { get; set; } //list to hold all step descriptions
    [Display(Name = "Step title/overview")]
    public string step { get; set; } //string to hold step title
    [Display(Name = "Step Description")]
    public string stepDescription { get; set; } //string to hold step description

    public Input()
    {
        //initialize lists
        Steps = new List<string>();
        StepDescriptions = new List<string>();
    }
}
}