2017-03-22 6 views
0

私はユーザーがレシピを作成できるようにするためのプログラムを作成しています。他の属性の中でも、レシピはいくつかのステップ/指示に関連付けられます。次のように私は、ビューを書いた:MVCのビューからのリストへのアクセスと変更

@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アクションメソッドが返されるとすぐにその要素が破棄されたようです。私がそれを実行するたびに、いくつの項目を追加しても、ビューは「ステップ数: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>(); 
    } 
} 
} 
+2

ウェブはステートレスです。要求が行われるたびに、コントローラの新しいインスタンスが作成され、その結果、 'Input input;'の新しいインスタンスが生成されます。 - データを(データベースなどの)永続化する必要があります。なぜフォームの外にある 'EditorFor()'によって生成されたすべての入力を持っていますか? –

+0

@StephenMuecke私はこれが起こっていたような何かを感じた。それはとても役に立ちます、ありがとうございます。あなたの質問に答えるために、Inputのリストにアクセスしていることを確認するために、BeginForm行を書きました。私は今それを変更するつもりです。ありがとう! – AustinC

答えて

0

...モデルが重要であるかもしれないと仮定します。

Inputクラスは何か分かりませんが、理想的にはViewのモデルとなるステップのリストが含まれています。それをAddStepメソッドにポストし、新しいステップを追加して、リストをビューに再レンダリングします。

関連する問題