2017-03-07 3 views
0

asp.net을 사용하여 bd에서 테이블로 채워진 모든 개체를 별도의 테이블 단락으로 나누는 방법은 아직 해결할 수 없지만 어떤 도움을 주시면 감사하겠습니다. 여기 지금 방법의 스크린 샷입니다 : enter image description hereASP.Net mvc5

그 내가이 웹에있을하는 방법 : 이것은 인덱스 페이지 내 표는

enter image description here

이다는

@{ 
    ViewBag.Title = "Главная страница"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<table> 
     @foreach (var b in ViewBag.Events) 
{ 
    <tr> 
     <td style=" padding: 8px;"><p><h2>@b.Date</h2></p></td> 
     <td style=" padding: 8px;"><p><h2>@b.Time</h2></p></td> 
    </tr> 
} 
    <tr>  
     <td style="border: solid 1px grey; color: red; text-align: center; "><p><h3>Title</h3></p></> 
     <td style="border: solid 1px grey; color: red; text-align: center; "><p><h3>Place</h3></p></td> 
     <td style="border: solid 1px grey; color: red; text-align: center; "><p><h3>Lecturer</h3></p></td> 
    </tr> 

    @foreach (var b in ViewBag.Events) 
{ 
    <tr style="padding: 80px;"> 
     <td style="border: solid 1px grey; padding: 8px; background-color: #ddc9ee;"><p><h4>@b.Title</h4></p></td> 
     <td style="border: solid 1px grey; padding: 8px; background-color: #ddc9ee; "><p><h4>@b.Location</h4></p></td> 
     <td style="border: solid 1px grey; padding: 8px; background-color: #ddc9ee; "><p><h4>@b.Responsible</h4></p></td> 

    </tr> 
} 
</table> 


<a href="CreateEvent" class="Button">Add Event</a> 

홈 컨트롤러

using PhClub.Models; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace PhClub.Controllers 
{ 
    public class HomeController : Controller 
    { 
     EventsContext db = new EventsContext(); 

     public ActionResult Index() 
     { 

      IEnumerable<Event> events = db.Events; 
      ViewBag.Events = events; 

      return View(); 

     } 
     [HttpGet] 
     public ActionResult CreateEvent() 
     { 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult CreateEvent(AddEvent addEvent) 
     { 
      db.AddEvents.Add(addEvent); 
      db.SaveChanges(); 

      return View(); 
     } 
    } 
    } 

두 번째 질문 - 현재 ID가 하드 코드 된 ID로 새 이벤트를 추가 할 때 위의 방법으로 채워지지 않습니다 - 어디에서 잘못 되었습니까?

여기,

<body> 
    <div> 
     <h3>Form of event creation</h3> 
     <form method="post" action=""> 
      @*<input type="hidden" value="@ViewBag.AddEventId" name="AddEventId" />*@ 
      <table> 
       <tr> 
        <td><p>Enter Id of event :</p></td> 
        <td><input type="text" name="AddEventId" /> </td> 
       </tr> 
       <tr> 
        <td><p>Enter title :</p></td> 
        <td><input type="text" name="Title" /> </td> 
       </tr> 
       <tr> 
        <td><p>Date:</p></td> 
        <td><input type="text" name="Date" /> </td> 
       </tr> 
       <tr> 
        <td><p>Time :</p></td> 
        <td><input type="text" name="Time" /> </td> 
       </tr> 
       <tr> 
        <td><p>Address :</p></td> 
        <td> 
         <input type="text" name="Location" /> 
        </td> 
       <tr> 
        <td><p>Lecturer:</p></td> 
        <td><input type="text" name="Responsible" /> </td> 
       </tr>     
       <tr><td><input type="submit" value="Send" /> </td><td></td></tr> 
      </table> 
     </form> 
    </div> 
</body> 
내가 사용하는 이벤트 입력에 대한 테이블

답변

0

현재 구조는 인접한 두 루프 있습니다

@foreach (var b in ViewBag.Events) 
{ 
    [some output] 
} 
[more output] 
@foreach (var b in ViewBag.Events) 
{ 
    [even more output] 
} 

그래서 두 번 이벤트를 통해 반복하고를, 효과적으로 중간에 그 출력에 의해 분할 된 테이블의 두 개의 다른 섹션을 구축합니다. 그것은 단지 하나의 루프를 원했던 것처럼 보입니다. 루프의 각 반복에는 테이블 출력의 몇 행이 있습니다. 더이 같은 구조 뭔가 :

@foreach (var b in ViewBag.Events) 
{ 
    [some output] 
    [more output] 
    [even more output] 
} 

이 물론 검증되지 않은,하지만 아마도 코드에서 그 결과는 다음과 같습니다 번 기록을 통해

@foreach (var b in ViewBag.Events) 
{ 
    <tr> 
     <td style=" padding: 8px;"><p><h2>@b.Date</h2></p></td> 
     <td style=" padding: 8px;"><p><h2>@b.Time</h2></p></td> 
    </tr> 
    <tr>  
     <td style="border: solid 1px grey; color: red; text-align: center; "><p><h3>Title</h3></p></> 
     <td style="border: solid 1px grey; color: red; text-align: center; "><p><h3>Place</h3></p></td> 
     <td style="border: solid 1px grey; color: red; text-align: center; "><p><h3>Lecturer</h3></p></td> 
    </tr> 
    <tr style="padding: 80px;"> 
     <td style="border: solid 1px grey; padding: 8px; background-color: #ddc9ee;"><p><h4>@b.Title</h4></p></td> 
     <td style="border: solid 1px grey; padding: 8px; background-color: #ddc9ee; "><p><h4>@b.Location</h4></p></td> 
     <td style="border: solid 1px grey; padding: 8px; background-color: #ddc9ee; "><p><h4>@b.Responsible</h4></p></td> 
    </tr> 
} 

기본적으로, 단지 루프 출력 당신을 구축 각 레코드마다 필요합니다.

은 (두 번째 질문의 경우, 혼란을 방지하고 물건을 유지하기 위해. 새로운 스택 오버플로 질문을 열려는 한 번에 하나 개의 질문을 일반적으로 가장 좋은 간결 수 있습니다.)

+0

그것을 않았다! 데이빗 감사합니다. –