2016-11-18 15 views
0

詳細ビューに表示されたプロパティを使用してオブジェクトビューモデルからプロパティを取得しようとしています。取得しようとしているプロパティは、私のビューに記載されているsessionIdです。その値を引き出すコードが正しい値を返さない。デバッグ時にsessIDの値が0になる。私が間違っていることは何ですか?モデルのプロパティを「詳細」から取得するASP.Net MVC

マイモデル

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

マイビューフォームが戻って任意の値を渡していない

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

答えて

2

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

私の行動。これらを直接編集したくない場合は、@Html.HiddenFor(...)を使用する必要があります。 DisplayFor()は、フォームにテキストを追加するだけで、必要な<input>要素は生成されません。

+0

ありがとうジャセン。私はDisplayFor()とHiddenFor()の両方を使用して終了しました。 DisplayFor()は値を表示し、HiddenFor()は実際に値を自分のアクションに戻します。 – tmt32mj

関連する問題