Link to home
Start Free TrialLog in
Avatar of mustish1
mustish1

asked on

Edit or Detail link

VS 2019

I got an error when I hit the Edit or Detail link. Can you please help me out?
User generated image

User generated image

User generated image


Avatar of Jonathan D.
Jonathan D.
Flag of Israel image

I see that the view is accepting a data model of type Bks_DWH_TrainingProgramsBasicData. Can you show how the action for this particular view is being handled inside the controller? Despite that, it could also be a problem with your view's code. I could suggest you go with the "narrow down" strategy to tackle the problem by removing nearly everything from the view's code and returning piece by piece until you get to the point that the error returns again.


After this experiment, please get back here with the results so we can resolve your issue.

Avatar of mustish1
mustish1

ASKER

SchedulingController
---------------------

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.Entity;

using System.Linq;

using System.Net;

using System.Web;

using System.Web.Mvc;

using Scheduling.Models;


namespace Scheduling.Controllers

{

    public class SchedulingController : Controller

    {

        private SchedulingContext db = new SchedulingContext();


        // GET: Scheduling

        public ActionResult Index()

        {

            return View(db.Bks_DWH_TrainingProgramsBasicData.ToList());

        }


        // GET: Scheduling/Details/5

        public ActionResult Details(int? id)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }

            Bks_DWH_TrainingProgramsBasicData bks_DWH_TrainingProgramsBasicData = db.Bks_DWH_TrainingProgramsBasicData.Find(id);

            if (bks_DWH_TrainingProgramsBasicData == null)

            {

                return HttpNotFound();

            }

            return View(bks_DWH_TrainingProgramsBasicData);

        }


        // GET: Scheduling/Create

        public ActionResult Create()

        {

            return View();

        }


        // POST: Scheduling/Create

        // To protect from overposting attacks, enable the specific properties you want to bind to, for 

        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.

        [HttpPost]

        [ValidateAntiForgeryToken]

        public ActionResult Create([Bind(Include = "TrainingProgramID,TrainingProgramName,TrainingProgramExternalID,TrainingProgramStartDate,TrainingProgramEndDate,TrainingProgramCurrentStatus,TrainingProgramPassingGrade,TrainingProgramNewMetaDataTypeID,TrainingProgramNewMetaDataTypeName,TrainingProgramMetaDataTypeID,TrainingProgramMetaDataTypeName,TrainingProgramTrainignDays,TrainingProgramIsPersonal,TrainingProgram_OrganizationalUnitID,TrainingProgram_OrganizationalUnitName,TrainingProgram_OrganizationalUnitExternalID,TrainingProgram_MasterPlanID,TrainingProgram_MasterPlanName,TrainingProgram_MasterPlanExternalID,TrainingProgram_LocationID,TrainingProgram_CustomerName,TrainingProgram_CustomerCode,TrainingProgramSchedulingMethod")] Bks_DWH_TrainingProgramsBasicData bks_DWH_TrainingProgramsBasicData)

        {

            if (ModelState.IsValid)

            {

                db.Bks_DWH_TrainingProgramsBasicData.Add(bks_DWH_TrainingProgramsBasicData);

                db.SaveChanges();

                return RedirectToAction("Index");

            }


            return View(bks_DWH_TrainingProgramsBasicData);

        }


        // GET: Scheduling/Edit/5

        public ActionResult Edit(int? id)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }

            Bks_DWH_TrainingProgramsBasicData bks_DWH_TrainingProgramsBasicData = db.Bks_DWH_TrainingProgramsBasicData.Find(id);

            if (bks_DWH_TrainingProgramsBasicData == null)

            {

                return HttpNotFound();

            }

            return View(bks_DWH_TrainingProgramsBasicData);

        }


        // POST: Scheduling/Edit/5

        // To protect from overposting attacks, enable the specific properties you want to bind to, for 

        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.

        [HttpPost]

        [ValidateAntiForgeryToken]

        public ActionResult Edit([Bind(Include = "TrainingProgramID,TrainingProgramName,TrainingProgramExternalID,TrainingProgramStartDate,TrainingProgramEndDate,TrainingProgramCurrentStatus,TrainingProgramPassingGrade,TrainingProgramNewMetaDataTypeID,TrainingProgramNewMetaDataTypeName,TrainingProgramMetaDataTypeID,TrainingProgramMetaDataTypeName,TrainingProgramTrainignDays,TrainingProgramIsPersonal,TrainingProgram_OrganizationalUnitID,TrainingProgram_OrganizationalUnitName,TrainingProgram_OrganizationalUnitExternalID,TrainingProgram_MasterPlanID,TrainingProgram_MasterPlanName,TrainingProgram_MasterPlanExternalID,TrainingProgram_LocationID,TrainingProgram_CustomerName,TrainingProgram_CustomerCode,TrainingProgramSchedulingMethod")] Bks_DWH_TrainingProgramsBasicData bks_DWH_TrainingProgramsBasicData)

        {

            if (ModelState.IsValid)

            {

                db.Entry(bks_DWH_TrainingProgramsBasicData).State = EntityState.Modified;

                db.SaveChanges();

                return RedirectToAction("Index");

            }

            return View(bks_DWH_TrainingProgramsBasicData);

        }


        // GET: Scheduling/Delete/5

        public ActionResult Delete(int? id)

        {

            if (id == null)

            {

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }

            Bks_DWH_TrainingProgramsBasicData bks_DWH_TrainingProgramsBasicData = db.Bks_DWH_TrainingProgramsBasicData.Find(id);

            if (bks_DWH_TrainingProgramsBasicData == null)

            {

                return HttpNotFound();

            }

            return View(bks_DWH_TrainingProgramsBasicData);

        }


        // POST: Scheduling/Delete/5

        [HttpPost, ActionName("Delete")]

        [ValidateAntiForgeryToken]

        public ActionResult DeleteConfirmed(int id)

        {

            Bks_DWH_TrainingProgramsBasicData bks_DWH_TrainingProgramsBasicData = db.Bks_DWH_TrainingProgramsBasicData.Find(id);

            db.Bks_DWH_TrainingProgramsBasicData.Remove(bks_DWH_TrainingProgramsBasicData);

            db.SaveChanges();

            return RedirectToAction("Index");

        }


        protected override void Dispose(bool disposing)

        {

            if (disposing)

            {

                db.Dispose();

            }

            base.Dispose(disposing);

        }

    }

}



HomeController.cs
-------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;


namespace Scheduling.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }


        public ActionResult About()

        {

            ViewBag.Message = "Your application description page.";


            return View();

        }


        public ActionResult Contact()

        {

            ViewBag.Message = "Your contact page.";


            return View();

        }

    }

}


Details.cshtml
--------------

@model Scheduling.Models.Bks_DWH_TrainingProgramsBasicData


@{

    ViewBag.Title = "Details";

}


<h2>Details</h2>


<div>

    <h4>Bks_DWH_TrainingProgramsBasicData</h4>

    <hr />

    <dl class="dl-horizontal">

        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramEndDate)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramEndDate)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramPassingGrade)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramPassingGrade)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramNewMetaDataTypeID)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramNewMetaDataTypeID)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramNewMetaDataTypeName)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramNewMetaDataTypeName)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramMetaDataTypeID)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramMetaDataTypeID)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramMetaDataTypeName)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramMetaDataTypeName)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramTrainignDays)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramTrainignDays)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgram_OrganizationalUnitExternalID)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgram_OrganizationalUnitExternalID)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgram_MasterPlanID)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgram_MasterPlanID)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgram_LocationID)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgram_LocationID)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgram_CustomerName)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgram_CustomerName)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgram_CustomerCode)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgram_CustomerCode)

        </dd>


        <dt>

            @Html.DisplayNameFor(model => model.TrainingProgramSchedulingMethod)

        </dt>


        <dd>

            @Html.DisplayFor(model => model.TrainingProgramSchedulingMethod)

        </dd>


    </dl>

</div>

<p>

    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |

    @Html.ActionLink("Back to List", "Index")

</p>


Edit.cshtml
------------

@model Scheduling.Models.Bks_DWH_TrainingProgramsBasicData


@{

    ViewBag.Title = "Edit";

}


<h2>Edit</h2>



@using (Html.BeginForm())

{

    @Html.AntiForgeryToken()

    

    <div class="form-horizontal">

        <h4>Bks_DWH_TrainingProgramsBasicData</h4>

        <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.TrainingProgramID)


        @Html.HiddenFor(model => model.TrainingProgramName)


        @Html.HiddenFor(model => model.TrainingProgramExternalID)


        @Html.HiddenFor(model => model.TrainingProgramStartDate)


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramEndDate, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramEndDate, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramEndDate, "", new { @class = "text-danger" })

            </div>

        </div>


        @Html.HiddenFor(model => model.TrainingProgramCurrentStatus)


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramPassingGrade, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramPassingGrade, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramPassingGrade, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramNewMetaDataTypeID, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramNewMetaDataTypeID, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramNewMetaDataTypeID, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramNewMetaDataTypeName, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramNewMetaDataTypeName, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramNewMetaDataTypeName, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramMetaDataTypeID, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramMetaDataTypeID, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramMetaDataTypeID, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramMetaDataTypeName, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramMetaDataTypeName, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramMetaDataTypeName, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramTrainignDays, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramTrainignDays, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramTrainignDays, "", new { @class = "text-danger" })

            </div>

        </div>


        @Html.HiddenFor(model => model.TrainingProgramIsPersonal)


        @Html.HiddenFor(model => model.TrainingProgram_OrganizationalUnitID)


        @Html.HiddenFor(model => model.TrainingProgram_OrganizationalUnitName)


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgram_OrganizationalUnitExternalID, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgram_OrganizationalUnitExternalID, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgram_OrganizationalUnitExternalID, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgram_MasterPlanID, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgram_MasterPlanID, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgram_MasterPlanID, "", new { @class = "text-danger" })

            </div>

        </div>


        @Html.HiddenFor(model => model.TrainingProgram_MasterPlanName)


        @Html.HiddenFor(model => model.TrainingProgram_MasterPlanExternalID)


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgram_LocationID, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgram_LocationID, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgram_LocationID, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgram_CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgram_CustomerName, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgram_CustomerName, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgram_CustomerCode, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgram_CustomerCode, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgram_CustomerCode, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            @Html.LabelFor(model => model.TrainingProgramSchedulingMethod, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.TrainingProgramSchedulingMethod, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.TrainingProgramSchedulingMethod, "", new { @class = "text-danger" })

            </div>

        </div>


        <div class="form-group">

            <div class="col-md-offset-2 col-md-10">

                <input type="submit" value="Save" class="btn btn-default" />

            </div>

        </div>

    </div>

}


<div>

    @Html.ActionLink("Back to List", "Index")

</div>


@section Scripts {

    @Scripts.Render("~/bundles/jqueryval")

}


I see the problem: inside the Create action, you're returning the view without passing the data model with the information. You see, the view is expecting data from the model to render, and you're not passing it to it.


        // GET: Scheduling/Create
        public ActionResult Create()
        {
            return View();
        }

Open in new window


Pass the data model from the database or local storage as an argument.

How do I pass the data model?

ASKER CERTIFIED SOLUTION
Avatar of Jonathan D.
Jonathan D.
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial