[RESOLVED]create and index views combined in 1 view

I want to be able to add players and upon addition in the same I want to display them in a table similar to what page Index view offers. so what I did is combine the index and create views in 1 page.

but when debugging I’m have a resource cannot be found error.

can some one please correct where I’m going wrong

Index View:

@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);

        }
	}
}

lolo512

but when debugging I’m have a resource cannot be found error.

Please post the resource and the error your getting. A full stack trace helps.

pprasannak

Please post the resource and the error your getting. A full stack trace helps.

[/quote

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /player/Index

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

]

Hi lolo512,

Base on your code, you have HttpPost attribute in the Index action. So the request type should be Post. (e.g. form submit with post request, AJAX with post request)

Best Regards

Starain

Looks like ‘return View();’ inside create action method is causing the issue. It expects a view  with name Create.cshtml and you don’t have this. Please specify view name as given below

public ActionResult Create()

{

return View("Index");

}

IMO I don’t think your codes will work even if you fix the 404 error. You used IEnumerable<sportsMania.player> as your model which is for your Index. You need to have a model of type sportsMania.player for your Create. But you can’t have 2 @models in one
view so you should first learn how to use a ViewModel. Here’s a good link to start:

http://www.rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications

If change your code as in the example given below, you can achieve this

Model Code

public class Player
    {
        public string Name
        { get; set; }

        public int Poistion
        { get; set; }

        public string Email
        { get; set; }
    }

    public class PlayerViewModel
    {
        public Player Player
        { get; set; }

        public List<Player> PlayerList
        { get; set; }
    }

View

@model SportsMania.Models.PlayerViewModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@if (ViewBag.Action == "Index")
{
    <h2>Index</h2>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Player.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Player.Poistion)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Player.Email)
            </th>
        </tr>
        @foreach (var item in Model.PlayerList)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Poistion)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Email)
                </td>
            </tr>
        }
    </table>
}
else
{ 
    <h2>Create</h2>
    using(Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            <h4>category</h4>
            <hr />
            @Html.ValidationSummary(true)
            <div class="form-group">
                @Html.LabelFor(model => model.Player.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Player.Name)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Player.Poistion, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Player.Poistion)
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Player.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Player.Email)
                </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>
    }
}

Controller

public class PlayerController : Controller
    {
        // GET: Player
        public ActionResult Index()
        {
            PlayerViewModel vm = new PlayerViewModel();
            vm.PlayerList = db.players.ToList();
            ViewBag.Action = "Index";
            return View(vm);
        }

        public ActionResult Create()
        {
            ViewBag.Action = "Create";
            return View("Index");
        }

        [HttpPost]
        public ActionResult Create(PlayerViewModel model)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View("Index");
        }

    }

Leave a Reply