如何使用Html Helper使用多个编辑表单

时间:2012-10-25 20:41:14

标签: asp.net asp.net-mvc razor html-helper

我想制作多个编辑页面。 但我不知道如何在TextBoxFor()循环中使用TextAreaFor()ValidationMessageFor()foreach

@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" />
    }
}

上面的代码无法设置textarea值,我认为这不是正确的方法。

编辑)

我改变了这样的代码,

@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" />
    }
}

我仍然遇到使用ValidationMessageFor()的问题。

我只将一个内容设为空并提交表单然后就像这样,

enter image description here

我该如何将ValidationMessage放在正确的位置?

[编辑#2]

是的,我也在同一视图中创建了表单,

View代码是这样的,

@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" />
    }
}

模型,

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; }

}

查看模型,

public class NoteViewModel
{
    public IEnumerable<Note> noteList { get; set; }
    public Note note { get; set; }
}

控制器,

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() });
}

2 个答案:

答案 0 :(得分:3)

如果需要,您可以实际执行此操作(不需要使用model):

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

有时我将lambda变量名称更改为_,以显示model并不重要(但如果你愿意,它是可选的):

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

答案 1 :(得分:0)

由于你在每个语句中迭代“note”var,你的html助手会引用“note”var而不是模型。解决方案如下:

@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(note => note.userId)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(note => note.userId, new { @Value = note.userId })
            @Html.ValidationMessageFor(note => note.userId)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(note => note.noteList)
            @Html.ValidationMessageFor(note => note.content)
        </div>
        <input type="submit" value="Edit" />
    }
}