2 Replies - 7173 Views - Last Post: 20 December 2010 - 07:31 AM

#1 erburrell   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 22-December 09

Model.Id not showing in MVC View Page

Posted 13 July 2010 - 10:08 AM

I have a web page that I am developing in ASP.NET MVC. I have one page that is being opened to allow the user to edit some data. I have the data stored in a couple of tables, and when there is a "trip", the user needs to add the data for that trip. I first add the record to the table, and then have them edit that record. However, when my edit view comes up, it is dropping off the model.id and the page shows that the Id is required. Any help is greatly appreciated.

I am also using Fluent NHibernate, and have verified that the record is created, and has a value for Id.

My record model:
 public class CtTrip
    {
        public virtual int Id { get; set; }
        public virtual int CtStatId { get; set; }
        public virtual DateTime TripTime { get; set; }
        public virtual decimal TripLoad { get; set; }
        public virtual string Comment { get; set; }
    }


My Controller:
public class LTSAController : Controller
    {

        // Class Parameters
        DateTime minDate = DateTime.FromOADate(1.0);

        //
        // GET: /LTSA/

        public ActionResult Index()
        {
            return View();
        }

        //View all CtStats in List Form:

        public ActionResult CtList()
        {
            ViewData["Message"] = "Cumbustion Turbine Event Listing";

            var ctListing = CtStatBLL.GetCtStatQueryList();

            return View(ctListing);
        }

        //Edit a CtStat...
        public ActionResult CtEdit(int id)
        {
            var stat = CtStatBLL.GetCtStatById(id);

            return View(stat);
        }

        [HttpPost]
        public ActionResult CtEdit(CtStat stat)
        {
            try
            {
                CtStatBLL.UpdateCtStat(stat);

                ViewData["Message"] = "Update successful.";

                var ctStats = CtStatBLL.GetCtStatQueryList();

                return View("CtList", ctStats);
            }
            catch (Exception e)
            {
                ViewData["Message"] = "Update Failed: " + e.Message;

                var ctStats = CtStatBLL.GetCtStatQueryList();

                return View("CtList", ctStats);
            }
        }

        // Create a new CtStat
        public ActionResult CtCreate()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CtCreate(CtStat stat)
        {
            string errorMsg = string.Empty;
            try
            {
                if (stat.Unit < 1 || stat.Unit > 3)
                    throw new Exception("Invalid Unit");

                if (stat.StartTime < minDate)
                    throw new Exception("Start Time must be entered.");

                if (stat.FireTime < minDate)
                    stat.FireTime = minDate;

                if (stat.SyncTime < minDate)
                    stat.SyncTime = minDate;

                if (stat.BreakerOpenTime < minDate)
                    stat.BreakerOpenTime = minDate;

                if (stat.EndTime < minDate)
                    stat.EndTime = stat.StartTime;

                CtStatBLL.InsertCtStat(stat);

                ViewData["Message"] = "CT Start Data Added.";

                CtTrip newTrip = new CtTrip();
                newTrip.CtStatId = stat.Id;
                newTrip.TripTime = stat.EndTime;
                newTrip.TripLoad = 0;
                newTrip.Comment = string.Empty;
                CtTripBLL.InsertCtTrip(newTrip);
                newTrip = CtTripBLL.GetCtTripByCtStatId(stat.Id);

                return View("TripEdit", newTrip);
            }
            catch (Exception e)
            {
                ViewData["Message"] = "Insert Failed: " + e.Message;

                var ctStats = CtStatBLL.GetCtStatQueryList();

                return View("CtList", ctStats);
            }
        }
    }


My TripEdit View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MagnoliaWeb.Entities.CtTrip>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	TripEdit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>TripEdit</h2>

    <% using (Html.BeginForm()) {%>
        <%= Html.ValidationSummary(true) %>
        
        <fieldset>
            <legend>Fields</legend>
            
            <div class="editor-label">
                <%= Html.LabelFor(model => model.Id) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Id) %>
                <%= Html.ValidationMessageFor(model => model.Id) %>
            </div>
            
            <div class="editor-label">
                <%= Html.LabelFor(model => model.CtStatId) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.CtStatId) %>
                <%= Html.ValidationMessageFor(model => model.CtStatId) %>
            </div>
            
            <div class="editor-label">
                <%= Html.LabelFor(model => model.TripTime) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.TripTime, String.Format("{0:g}", Model.TripTime)) %>
                <%= Html.ValidationMessageFor(model => model.TripTime) %>
            </div>
            
            <div class="editor-label">
                <%= Html.LabelFor(model => model.TripLoad) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.TripLoad, String.Format("{0:F}", Model.TripLoad)) %>
                <%= Html.ValidationMessageFor(model => model.TripLoad) %>
            </div>
            
            <div class="editor-label">
                <%= Html.LabelFor(model => model.Comment) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Comment) %>
                <%= Html.ValidationMessageFor(model => model.Comment) %>
            </div>
            
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%= Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="FooterContent" runat="server">
</asp:Content>




Is This A Good Question/Topic? 0
  • +

Replies To: Model.Id not showing in MVC View Page

#2 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Model.Id not showing in MVC View Page

Posted 18 December 2010 - 07:04 AM

If I remember correctly, I believe this is a security precaution taken by the ASP.NET MVC Development team. Exposing database result Id's to the end user is almost always a bad thing to do ( not really my opinion, but I understand it and can relate to it ). Therefore the default templates introduced in ASP.NET MVC 2 does not output any properties by the name Id.

I might be completely wrong since I can't remember my reference for this information, but I recall reading something about this somewhere some time ago. :P

One solution would be to provide your own templates, or alternatively rename the property to something else.

Hope it helps! :)
Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Model.Id not showing in MVC View Page

Posted 20 December 2010 - 07:31 AM

This post is old, but I'll answer because it might help Googler's who come into the thread.

This code right here:

Inherits="System.Web.Mvc.ViewPage<MagnoliaWeb.Entities.CtTrip>"


Tell's your code what your "Model" keyword IS. So far so good. When you use the Model keyword in your code, it'll actually be a CtTrip object. Pretty neat!

This code here, tells me that the fields you defined in your Model "CtTrip" SHOULD be rendered as a textbox:

<div class="editor-label">
    <%= Html.LabelFor(model => model.Id) %>
</div>

<div class="editor-field">
    <%= Html.TextBoxFor(model => model.Id) %>
    <%= Html.ValidationMessageFor(model => model.Id) %>
</div>


Can you explain what the problem is?

What are you getting vs. what you want to get?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1