如何使用ASP.Net mvc5

时间:2017-03-07 13:15:29

标签: asp.net-mvc-5

学习asp.net并遇到了一个我无法解决的问题,任何帮助都会受到赞赏。 以下是现在的屏幕截图: enter image description here

以及我希望它如何在网上:

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

第二个问题 - 目前,当我向Db添加具有硬编码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>

1 个答案:

答案 0 :(得分: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>
}

基本上,只需循环记录一次,然后为每条记录构建所需的输出。

(对于你的第二个问题,你可能想要打开一个新的Stack Overflow问题。为避免混淆并保持简洁,通常最好一次提出一个问题。)