Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use multiple edit form using Html Helper
    text
    copied!<p>I want to make multiple editing page. But I don't know how to use <code>TextBoxFor()</code>, <code>TextAreaFor()</code>, <code>ValidationMessageFor()</code> in a <code>foreach</code> loop.</p> <pre><code>@foreach (var note in Model.noteList) { using(Html.BeginForm()){ @Html.Hidden("id", note.id); &lt;div class="userIdArea"&gt;&lt;b&gt;@note.userId&lt;/b&gt;&lt;/div&gt; &lt;div class="noteArea"&gt;@note.content&lt;/div&gt; &lt;br /&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(model =&gt; model.note.userId, new { @Value = note.userId }) @Html.ValidationMessageFor(model =&gt; model.note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextAreaFor(note =&gt; note.noteList) @Html.ValidationMessageFor(model =&gt; model.note.content) &lt;/div&gt; &lt;input type="submit" value="Edit" /&gt; } } </code></pre> <p>The code above cannot set the textarea value, and I don't think it's the right way to do that.</p> <p>EDIT )</p> <p>I changed code like this,</p> <pre><code>@foreach (var note in Model.noteList) { using(Html.BeginForm()){ @Html.Hidden("note.id", note.id); &lt;div class="userIdArea"&gt;&lt;b&gt;@note.userId&lt;/b&gt;&lt;/div&gt; &lt;div class="noteArea"&gt;@note.content&lt;/div&gt; &lt;br /&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(model =&gt; note.userId) @Html.ValidationMessageFor(model =&gt; note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextAreaFor(model =&gt; note.content) @Html.ValidationMessageFor(_ =&gt; note.content) &lt;/div&gt; &lt;input type="submit" value="Edit" /&gt; } } </code></pre> <p>and I still have problem with using ValidationMessageFor().</p> <p>I make only one content empty and submit form then it happens like this,</p> <p><img src="https://i.stack.imgur.com/snCKK.jpg" alt="enter image description here"></p> <p>How should I do put ValidationMessage on right place?</p> <p>[EDIT #2]</p> <p>Yes, I have create form too in same view,</p> <p>the View code is like this,</p> <pre><code>@model MemoBoard.Models.NoteViewModel @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Note&lt;/h2&gt; &lt;br /&gt;&lt;br /&gt; @using(Html.BeginForm()){ &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(model =&gt; model.note.userId) @Html.ValidationMessageFor(model =&gt; model.note.userId) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.note.content) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextAreaFor(model =&gt; model.note.content, new { rows = 4}) @Html.ValidationMessageFor(model =&gt; model.note.content) &lt;/div&gt; &lt;input type="submit" value ="Save" /&gt; } @* //End create form *@ &lt;!-- List Area --&gt; @foreach (var note in Model.noteList) { using(Html.BeginForm()){ @Html.Hidden("note.id", note.id); @Html.EditorForModel() &lt;div class="userIdArea"&gt;&lt;b&gt;@note.userId&lt;/b&gt;&lt;/div&gt; &lt;div class="noteArea"&gt;@note.content&lt;/div&gt; &lt;br /&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; note.userId, new { id = "A"}) @Html.ValidationMessageFor(model =&gt; note.userId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.TextAreaFor(model =&gt; note.content) @Html.ValidationMessageFor(model =&gt; note.content) &lt;/div&gt; &lt;input type="submit" value="Edit" /&gt; } } </code></pre> <p>And,</p> <p>Model,</p> <pre><code>public class Note { [Key] public int id { get; set; } [Required(ErrorMessage="Content is required")] [DisplayName("Note")] public string content { get; set; } public DateTime date { get; set; } [Required(ErrorMessage = "User ID is required")] [DisplayName("User ID")] public string userId {get; set;} public Boolean isPrivate { get; set; } public virtual ICollection&lt;AttachedFile&gt; AttachedFiles { get; set; } } </code></pre> <p>View Model,</p> <pre><code>public class NoteViewModel { public IEnumerable&lt;Note&gt; noteList { get; set; } public Note note { get; set; } } </code></pre> <p>Controller,</p> <pre><code>public ActionResult Index() { var notes = unitOfWork.NoteRepository.GetNotes(); return View(new NoteViewModel(){noteList=notes.ToList(), note = new Note()}); } [HttpPost] public ActionResult Index(Note note) { try { if (ModelState.IsValid) { unitOfWork.NoteRepository.InsertNote(note); unitOfWork.Save(); return RedirectToAction("Index"); } }catch(DataException){ ModelState.AddModelError("", "Unable to save changes. Try again please"); } var notes = unitOfWork.NoteRepository.GetNotes(); return View(new NoteViewModel() { noteList = notes.ToList(), note = new Note() }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload