私が持っている問題を明確に述べるのは難しいです。検証が失敗した後にループで作成されたフォームフィールドに値を保持する方法を理解しようとしています。私はループと検証で作成された要素の束を持っているより複雑な現実のフォームを持っています。私はそれを以下の簡単な例に減らしました。
検証に失敗した場合は、ループ内に作成された「コメント」という名前のテキストエリアを使用して、以下の事前送信イメージに示されている値を保持します。
フォーム提出をデバッグすると、各フィールドの値は、ModelにあるCommentという名前のIList変数に正常に接続されます。これは私がループし、インデックスに基づいてそれらを見つけることができるようにしたいものです。
送信後、ループによって生成された各テキストエリアは、モデル内のIList変数コメントのコンマ区切りの表示を示します。ビュー内とモデル内のフィールドは、名前を共有しているため、接続しているようです。彼らは途中で適切に接続しますが、途中では接続しません。ビュー全体ではなく、コメント[i]に関連付けられた値のみを表示して、フォーム提出の間に値が一定になるようにしたいと思います。
最初のロード以下スクリーンショットとサンプルコード:
事前提出フォームの変更点:最初の提出後に見られるように
フォーム:
秒提出後に見られるようなフォーム:
モデルのコード
MVC Razor ViewのHTML.Textareaの値
using System.Collections.Generic;
namespace UI.Models.Forms
{
public class TempListModel : ContentModel
{
public TempListModel()
{
Comment = new List<string>();
}
public IList<string> Comment { get; set; } //Comments for each URL in the list
}
}
コードの表示
@model UI.Models.Forms.TempListModel
@using (Html.BeginForm("temptest", "Test", new { id = 1 }, FormMethod.Post, new { id = "listForm", name = "listForm" }))
{
<ul>
@for (int i = 0; i < Model.Comment.Count(); i++)
{
<li>
<div class="llformlabel">
Notes:
<div>@Model.Comment[i]</div>
@Html.TextArea("Comment", Model.Comment[i], 4, 63, new { @id = "Comment_" + i, @title = "Comment" })</div>
</li>
}
</ul>
<input type="submit" value="Save Changes" />
}
コントローラコード
using System.Collections.Generic;
using System.Web.Mvc;
using UI.Models.Forms;
namespace UI.Controllers
{
public class TestController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TempTest(TempListModel model)
{
//This function executes after the user submits the form.
//If server side validation fails then the user should be shown the form as it was when they submitted.
//model.Comment = GetComments(); //In my real world example this comes from a database.
if (true) //!ModelState.IsValid) //In my real world code this is a validation step that may fail
{
return View(model);
}
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult TempTest(int? id)
{
//In the real world example there is a lot going on in this function.
//It is used to load data from databases and set up the model to be displayed.
var model = new TempListModel();
model.Comment = GetComments();
return View("TempTest", "TempLayout", model);
}
private static IList<string> GetComments()
{
//Simple sample function used for demo purposes.
IList<string> comments = new List<string>();
comments.Add("Comment 1");
comments.Add("Comment 2");
comments.Add("Comment 3");
return comments;
}
}
}
RedirectToActionの代わりにビューを使用するようにコードサンプルを変更したので、GetComments関数が値を上書きしていないことがより明確になりました。私は現在のコードサンプルが示唆しているように表示していると思いますが、それと同じ問題があります。 – RacerNerd
@RacerNerd編集を参照してください。 – Jasen
ありがとうございます。これはトリックでした。それは、モデルの変数に接続するhtmlフィールドを必要とするたびに、 "For"でhtmlジェネレータを使用する方が適切ですか?私が作業しているコードは、変数の接続はほとんどがここで見ているようにすべての場合にうまくいかないような名前の一致によって実現されているようです。 – RacerNerd