tmt32mj
tmt32mj

Reputation: 5

Getting model property from "Details" View in ASP.Net MVC

I'm trying to get the property from an object view model with its properties shown in a details view to my action. The property I'm trying to get is the sessionId, which is listed in my view. My code to pull its value out does not return the correct value, The sessID has a value of 0 when debugged. What I am doing wrong?

My Model

    public class SessionViewModel    
    {
        public SessionViewModel()
        {

        }

        public SessionViewModel(Session sessionDTO)
        {
            Id = sessionDTO.Id;
            SessionTitle = sessionDTO.SessionTitle;
            SessionDescription = sessionDTO.SessionDescription;
            SessionPresenter = sessionDTO.SessionPresenter;
            SessionLocation = sessionDTO.SessionLocation;
            SessionRoom = sessionDTO.SessionRoom;
            SessionDate = sessionDTO.SessionDate;
            SessionOccupancy = sessionDTO.SessionOccupancy;
        }

        public int Id { get; set; }
        public string SessionTitle { get; set; }
        public string SessionDescription { get; set; }
        public string SessionPresenter { get; set; }
        public string SessionLocation { get; set; }
        public string SessionRoom { get; set; }
        public DateTime SessionDate { get; set; }
        public int SessionOccupancy { get; set; }
        public bool IsSelected { get; set; }
    }

My View

    @model Project.Models.ViewModels.ManageSession.SessionViewModel
    @{
        ViewBag.Title = "SessionDetails";
    }
    <h2>Session Details</h2>
    @using (Html.BeginForm("RegisterFromDetailsVM", "Session"))
    {
         <button type="submit" class="btn btn-success">Register</button>
         <div>
            <hr />
            <dl class="dl-horizontal">
            <dt>Session ID</dt>
            <dd>
                @Html.DisplayFor(model => model.Id)
            </dd>
            <dt>Title</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionTitle)
            </dd>
            <dt>Description</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionDescription)
            </dd>
            <dt>Presenter</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionPresenter)
            </dd>
            <dt>Location</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionLocation)
            </dd>
            <dt>Room</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionRoom)
            </dd>
            <dt>Date</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionDate)
            </dd>
            <dt>Occupancy</dt>
            <dd>
                @Html.DisplayFor(model => model.SessionOccupancy)
            </dd>
            </dl>
        </div>
    }

My action

    [HttpPost]
    public ActionResult RegisterFromDetailsVM(SessionViewModel sessionVM)
    {            
        using (WSADDbContext context = new WSADDbContext())
        {                
            //Capture logged in user info
            int userID = context.Users.Where(x => x.Username == User.Identity.Name).Select(y => y.Id).FirstOrDefault();
            string userName = context.Users.Where(x => x.Username == User.Identity.Name).Select(y => y.Username).FirstOrDefault();
            string firstName = context.Users.Where(x => x.Username == User.Identity.Name).Select(y => y.FirstName).FirstOrDefault();
            string lastName = context.Users.Where(x => x.Username == User.Identity.Name).Select(y => y.LastName).FirstOrDefault();

            //Get the sessionId
            int sessID = sessionVM.Id;                

            //Create the SubscribedSession DTO object
            context.SubscribedSessions.Add(new SubscribedSession()
            {
                UserID = userID,
                SessionID = sessID,
                FirstName = firstName,
                LastName = lastName,
                Username = userName
            });

            //Save the changes
            context.SaveChanges();
        }

        //return
        return RedirectToAction("Index");
    }

Upvotes: 0

Views: 224

Answers (1)

Jasen
Jasen

Reputation: 14250

Your form is not passing any values back. You will need to use @Html.HiddenFor(...) if you don't want these to be directly edited. DisplayFor() only adds text to the form but these will not generate a necessary <input> element.

Upvotes: 2

Related Questions