从MVC中的视图访问和修改列表

时间:2017-03-22 20:53:07

标签: 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>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>
}

控制器:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Recipe.Models;

namespace Recipe.Controllers
{
public class InputController : Controller
{

    public static int stepCount { get; set; }
    private Input input;

    public InputController()
    {
        input = new Input();
        stepCount = 0;
    }
    // GET: Input
    public ActionResult Index()
    {
        return View(input);
    }

    public ActionResult AddStep(string step, string stepDescription)
    {
        //called when button is pressed
        input.Steps.Add(step); //add input step to list
        input.StepDescriptions.Add(stepDescription); //add input description to list
        Debug.WriteLine(input.Steps.Count());

        stepCount++;
        Debug.WriteLine(stepCount);
        return RedirectToAction("Index", input);
    }
}
}

(我为奇怪的格式道歉,它奇怪地复制了。) 我发现,鉴于此设置,我对视图中的列表的访问权限非常有限。我可以在列表中添加一个元素,但似乎只要AddStep操作方法返回就会丢弃它。每次运行时,无论我添加多少项,视图都会打印出&#34;步数:1&#34;我错过了什么,或者这是一种访问列表的不正确方式?

提前致谢。

编辑:我认为模型可能很重要......

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>();
    }
}
}

1 个答案:

答案 0 :(得分:0)

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

我不确定Input类是什么,但理想情况下它会包含一个可作为View模型的步骤列表。然后,您可以将其发布到AddStep方法,添加新步骤,然后将列表重新呈现给视图。