简体   繁体   English

从ASP.Net MVC中的“详细信息”视图获取模型属性

[英]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. 我要获取的属性是sessionId,该属性在我的视图中列出。 My code to pull its value out does not return the correct value, The sessID has a value of 0 when debugged. 我提取其值的代码未返回正确的值,调试时sessID的值为0。 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");
    }

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. 如果您不希望直接对其进行编辑,则需要使用@Html.HiddenFor(...) DisplayFor() only adds text to the form but these will not generate a necessary <input> element. DisplayFor()仅将文本添加到表单,但不会生成必要的<input>元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM