2012-10-25 103 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