0

データ モデルの外部キーである 2 つの URL パラメーターを受け取るコントローラー アクションがあります。

 public ActionResult Create(SurveyResponseModel surveyresponsemodel, int MemberId, int ProgramId)
        {
            surveyresponsemodel.MemberId = MemberId;
            surveyresponsemodel.ProgramId = ProgramId;
            return View(surveyresponsemodel);
        } 

データモデルは次のとおりです。

public class SurveyResponseModel
    {
        [Key]
        public int ResponseId { get; set; }

        public int MemberId { get; set; }

        public int ProgramId { get; set; }

        // "If yes, what changes did you make? Mark all that apply."

        [DisplayName("Did you make any changes in your practice, research, or administration activities as a result of participating in this CME activity?")]
        public string CmeChanges { get; set; }

        [DisplayName("Better patient follow-up")]
        public bool PatientFollowUp { get; set; }

        public virtual SurveyProgramModel SurveyProgramModel { get; set; }

        public virtual PersonModel PersonModel { get; set; }

そして「SurveyProgramType」のデータモデル

 public class SurveyProgramModel
    {

        [Key]
        public int ProgramId { get; set; }

        public int ProgramYear { get; set; }

        public int ProgramStatusId { get; set; }

        public string ProgramTitle { get; set; }

        public int ProgramTypeId { get; set; }

        public virtual SurveyProgramTypeModel ProgramType { get; set; }

        public virtual ProgramStatusModel ProgramStatusModel { get; set; }


    }

私のビューでできるようにしたいのは、ProgramTitle渡された URL パラメータで を取得することですProgramId。したがって、ビューは次のようになります。

<div class="editor-label">
            @Model.SurveyProgramModel.ProgramTitle
        </div>

ただし、 @Model.SurveyProgramModel.ProgramTitle は null であるため例外をスローしています。ナビゲーション プロパティが正しく設定されていないと思います。それが何であるか分かりますか?

4

2 に答える 2

1

ビューモデルをビューに戻してはいけませんか?

public ActionResult Create(
    SurveyResponseModel surveyresponsemodel) //, int MemberId, int ProgramId)
{
    // MemberId and ProgramId arguments do not need to be defined
    // They will be picked up my MVC model binder, since there are properties
    // with the same name in SurveyResponseModel class
    //surveyresponsemodel.MemberId = MemberId;
    //surveyresponsemodel.ProgramId = ProgramId;
    surveyresponsemodel.SurveyProgramModel = new SurveyProgramModel(); // new line
    return View(surveyresponsemodel); // <- return your view model here
} 
于 2012-07-02T15:35:48.667 に答える
0

モデルをビューに渡さないと、ビュー内のモデルのプロパティにアクセスできません。それがエラーの考えられる原因です。

            public ~ActionResult PassModel(DemoModel _model, int id)
             {
              // your code goes here....

             return View(_model);   // pass the model to view ..so you can work on your model 
              }
于 2012-07-02T15:46:22.840 に答える