2015-08-27 12 views
5

Görüntünün doğru olarak etkinleştirilip etkinleştirilmediğini kontrol edemediğim görünümde bir kutu biçiminde bir boole (şu anda varsayılan olarak 0) görüntüleyen bir görünüm var. Kontrol ünitesine geri dönmek ve her iki değişikliği de bir tabloya kaydetmek için sonuç alanına metin girin. Birisi lütfen bu işlevin çalışmasına izin vermek için ne yapmam gerektiğini açıklayabilir mi?Boolean'ı etkinleştirin ve görünümünde metni girin ve ardından denetleyiciye geri dönün - MVC

enter image description here

Kontrolör kodu

public ActionResult P1A1Mark() 
    { 
     List<MarkModel> query = (from row in db.submits 
            where row.assignment_no.Equals("1") && row.group_no == 1 
            group row by new { row.assignment_no, row.student_no, row.student.firstname, row.student.surname } into g 
            select new MarkModel 
            { 
             student_no = g.Key.student_no, 
             student_surname = g.Key.surname, 
             student_firstname = g.Key.firstname 

            } 
             ).ToList(); 

     return View(query); 
    } 

Görünüm

@model IEnumerable<MvcApplication2.Models.MarkModel> 

@{ 
    ViewBag.Title = "P1A1Mark"; 
} 

<h2>Mark Student Assignments</h2> 

<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.student_no) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.student_surname) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.student_firstname) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.submitted) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.result) 
     </th> 

     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.student_no) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.student_surname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.student_firstname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.submitted) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.result) 
     </td> 

    </tr> 
} 

</table> 

Modeli

public class MarkModel 
{ 
    public string student_no { get; set; } 
    public string student_surname { get; set; } 
    public string student_firstname { get; set; } 
    public string assignment_no { get; set; } 
    public bool submitted { get; set; } 
    public string result { get; set; } 
    public Nullable<int> group_no { get; set; } 
} 
+0

'@ Html.DisplayFor (modelItem => item.result) -> @ Html.TextBoxFor (öğe => item.result)' – Igor

+0

sayesinde @Igor, boolean ile ne herhangi bir fikriniz var mı? – cg91

+0

Sanırım basit bir cevap için çok uzaktasınız. Kutuyu kontrol edip sonucu girseniz bile, görünümün çoğunu yeniden yazmanız ve tüm kontrol ünitesi kodlarını yazmanız gerekiyor. Nesnelerin bir listesini düzenlemenin birçok örneği vardır, 'Listeyi Düzenle MVC'yi aramak ve – JamieD77

cevap

1

türü için bir EditorTemplate oluştur.

/Views/Shared/EditorTemplates/MarkModel.cshtml

@model MvcApplication2.Models.MarkModel 
<tr> 
    <td>@Html.DisplayFor(m => m.student_no)</td> 
    <td>@Html.DisplayFor(m => m.student_surname)</td> 
    <td>@Html.DisplayFor(m => m.student_firstname)</td> 
    <td>@Html.CheckBoxFor(m => m.submitted)</td> 
    <td>@Html.TextBoxFor(m => m.result)</td> 
</tr> 

içinde ve ana görünümünde

@model IEnumerable<MvcApplication2.Models.MarkModel> 
@using (Html.BeginForm()) 
{ 
    <table> 
    <thead> 
     // add your th elements 
    </thead> 
    <tbody> 
     @Html.EditorFor(m => m) 
    <tbody> 
    </table> 
    <input type="submit" ../> 
} 

ve

[HttpPost] 
public ActionResult P1A1Mark(IEnumerable<MarkModel>model) 

geri göndermek için bir yöntem oluşturmaktır Alternatif görünümünde for döngü kullanabilir (model IList<T> olmalıdır)

for(int i = 0; i < Model.Count; i++) 
{ 
    .... 
    @Html.CheckBoxFor(m => m[i].submitted) 
} 
İlgili konular