I want to make multiple editing page.
But I don’t know how to use TextBoxFor(), TextAreaFor(), ValidationMessageFor() in a foreach loop.
@foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
@Html.Hidden("id", note.id);
<div class="userIdArea"><b>@note.userId</b></div>
<div class="noteArea">@note.content</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.note.userId, new { @Value = note.userId })
@Html.ValidationMessageFor(model => model.note.userId)
</div>
<div class="editor-field">
@Html.TextAreaFor(note => note.noteList)
@Html.ValidationMessageFor(model => model.note.content)
</div>
<input type="submit" value="Edit" />
}
}
The code above cannot set the textarea value, and I don’t think it’s the right way to do that.
EDIT )
I changed code like this,
@foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
@Html.Hidden("note.id", note.id);
<div class="userIdArea"><b>@note.userId</b></div>
<div class="noteArea">@note.content</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => note.userId)
@Html.ValidationMessageFor(model => note.userId)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => note.content)
@Html.ValidationMessageFor(_ => note.content)
</div>
<input type="submit" value="Edit" />
}
}
and I still have problem with using ValidationMessageFor().
I make only one content empty and submit form then it happens like this,

How should I do put ValidationMessage on right place?
[EDIT #2]
Yes, I have create form too in same view,
the View code is like this,
@model MemoBoard.Models.NoteViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Note</h2>
<br /><br />
@using(Html.BeginForm()){
<div class="editor-label">
@Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.note.userId)
@Html.ValidationMessageFor(model => model.note.userId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.note.content)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.note.content, new { rows = 4})
@Html.ValidationMessageFor(model => model.note.content)
</div>
<input type="submit" value ="Save" />
} @* //End create form *@
<!-- List Area -->
@foreach (var note in Model.noteList)
{
using(Html.BeginForm()){
@Html.Hidden("note.id", note.id);
@Html.EditorForModel()
<div class="userIdArea"><b>@note.userId</b></div>
<div class="noteArea">@note.content</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.note.userId)
</div>
<div class="editor-field">
@Html.EditorFor(model => note.userId, new { id = "A"})
@Html.ValidationMessageFor(model => note.userId)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => note.content)
@Html.ValidationMessageFor(model => note.content)
</div>
<input type="submit" value="Edit" />
}
}
And,
Model,
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<AttachedFile> AttachedFiles { get; set; }
}
View Model,
public class NoteViewModel
{
public IEnumerable<Note> noteList { get; set; }
public Note note { get; set; }
}
Controller,
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() });
}
You can actually do this if you want (don’t need to use the
model):Sometimes I change the lambda variable name to
_, to show that themodelis not important (but it’s optional if you want):