2012-10-25 38 views
0

使用多个编辑表单我想使多个编辑页面。 但我不知道如何在foreach循环使用TextBoxFor()TextAreaFor()ValidationMessageFor()如何使用HTML帮助

@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的值,我不认为这是正确的方式来做到这一点。

EDIT)

我改变了这样的代码,

@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]

是的,我有太多的相同视图中创建的形式,

查看代码是这样的,

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

回答

3

实际上,你可以做到这一点,如果你想要的(不需要使用model):

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

有时我改变λ变量名_,以表明model并不重要(但它是可选的,如果你想):

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

感谢您的回答,请您再次查看我编辑过的问题吗?验证消息仍然存在问题。 –

+0

我试过了我的自我,但对我来说它工作= /你必须有一些我们没有看到的其他代码,例如你有一个*保存*按钮和*编辑*按钮,但你的代码在问题中只有*编辑*按钮。 –

+0

我用完整的代码再次编辑了我的问题,请您再次查看我已编辑的(#2)问题吗?对不起,再次要求..谢谢 –

0

既然你迭代在了“笔记” VAR为每个HTML辅助将引用这句话“注意”变种,而不是模型。解决方案如下:

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

这会给我一个编译错误。您不能为foreach变量和lambda变量使用相同的名称。 –

+0

解决这个问题的方法之一是为笔记对象编写单独的编辑器模板,然后使用'@ Html.EditorFor(_ => Model.noteList)'。 Asp.Net足够聪明,可以查看集合并为您展开循环。我不确定这可以在页面上使用多个表单的效果如何 - 不会每个都激发页面的文章,然后需要重新加载? – Stark