在1个视图中组合的创建和索引视图

时间:2014-11-19 13:03:03

标签: model-view-controller

我希望能够添加播放器,并且在添加播放器时我希望将它们显示在类似于索引视图提供的页面的表中。所以我做的是组合索引并在1页中创建视图。

但是在调试时我找不到资源错误。

有人可以纠正我出错的地方

索引视图:

@model IEnumerable<sportsMania.player>
@{
ViewBag.Title = "Create";
}

<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>category</h4>
    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group">
        @Html.Label("Player Name")
        <div class="col-md-10">
            @Html.Editor("Player Name")

        </div>
    </div>
    <div class="form-group">
        @Html.Label("Team")
        <div class="col-md-10">
            @Html.DropDownList("team", ViewBag.team as SelectList)

        </div>
    </div>
    <div class="form-group">
        @Html.Label("Position")
        <div class="col-md-10">
            @Html.Editor("position")

        </div>
    </div>
    <div class="form-group">
        @Html.Label("Email")
        <div class="col-md-10">
            @Html.Editor("email")

        </div>
    </div>
    <div class="form-group">
        @Html.Label("Type")
        <div class="col-md-10">
            @Html.Editor("type")

        </div>
    </div>
    <div class="form-group">
        @Html.Label("Height")
        <div class="col-md-10">
            @Html.Editor("height")

        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

@{
 ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.playerName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.team)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.position)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.email)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.type)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.height)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.playerName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.team)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.position)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.email)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.type)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.height)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}

</table>

的PlayerController:

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

namespace sportsMania.Controllers
{
public class playerController : Controller

{
    sportsEntities db = new sportsEntities();
    [HttpPost]
    public ActionResult Index()
    {
        var data = from p in db.Teams

                   select p.teamName;

        SelectList list = new SelectList(data);
        ViewBag.team = list;

        return View(db.players.ToList());
    }
    public ActionResult Create()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Create(player player)
    {
        if (ModelState.IsValid)
        {
            player newRecord = new player();
            newRecord.playerName = Request.Form["Player Name"];
            newRecord.position = Request.Form["position"];
            newRecord.type =Request.Form[ "type"];
            newRecord.team =Request.Form[ "team"];
            newRecord.email = Request.Form["email"];
            newRecord.height = Request.Form["height"];
            db.players.Add(player);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(player);

    }
}
}

为什么我这样做?因为我不想每次用户必须添加一个播放器,他应该被重定向到创建视图,我想当用户完成添加播放器后,他会点击一个按钮&#34;完成添加&#34;并将被重定向回家。

0 个答案:

没有答案