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

时间:2016-11-18 21:04:58

标签: c# asp.net-mvc razor

我正在尝试从对象视图模型中获取属性,其属性显示在我的操作的详细信息视图中。我想要获取的属性是sessionId,它在我的视图中列出。我将其值拉出的代码不会返回正确的值。调试时sessID的值为0。我做错了什么?

我的模特

    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; }
    }

我的观点

    @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>
    }

我的行动

    [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");
    }

1 个答案:

答案 0 :(得分:2)

您的表单未传回任何值。如果您不希望直接编辑这些内容,则需要使用@Html.HiddenFor(...)DisplayFor()只会向表单添加文字,但这些文字不会生成必要的<input>元素。

相关问题