Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:24:15+00:00 2026-06-13T10:24:15+00:00

I want to make multiple editing page. But I don’t know how to use

  • 0

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,

enter image description here

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() });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:24:16+00:00Added an answer on June 13, 2026 at 10:24 am

    You can actually do this if you want (don’t need to use the model):

    @Html.LabelFor(model => note.userId)
    @Html.ValidationMessageFor(model => note.userId)
    @Html.ValidationMessageFor(model => note.content)
    

    Sometimes I change the lambda variable name to _, to show that the model is not important (but it’s optional if you want):

    @Html.LabelFor(_ => note.userId)
    @Html.ValidationMessageFor(_ => note.userId)
    @Html.ValidationMessageFor(_ => note.content)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I normally make multiple small edits to my source code, but I don't want
I really want to make use of multiple background support (Webkit, Firefox 3.6+), but
I want to make multiple page turn effect like flipboard. please give me suggestion
I want to make website in multiple languages, I do it right with cookies
I want to make a view where I can select multiple items from listview
in order to make things easier for users i want to add multiple keyword
I want to make multiple web requests for an external api using play 2.
i have a problem here, i want to make a multiple toolbar which works
I want to make multiple instances of this class. public class Worker { private
I'm using HRD because I want make changes to multiple entities within a single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.