【问题标题】:Accessing Navigation Properties in a View访问视图中的导航属性
【发布时间】:2012-07-02 15:32:45
【问题描述】:

我有一个控制器动作接收 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; }


    }

在我看来,我希望能够通过为ProgramId 传递的URL 参数检索ProgramTitle。所以视图看起来像:

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

但是,@Model.SurveyProgramModel.ProgramTitle 抛出异常,因为它为空。我在想我的导航属性设置不正确。知道那是什么吗?

【问题讨论】:

    标签: c# asp.net-mvc-3 entity-framework razor


    【解决方案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
    } 
    

    【讨论】:

    • 哎呀!很好的捕获,虽然它仍然抛出同样的错误。
    • 好的。所以现在您的 SurveyProgramModel 属性为空。您需要在将整个模型提供给视图之前对其进行初始化。
    • 它的语法是什么样的?如果我调用两个模型 ToList() 它希望我在我的视图中更改模型类型。
    • 我已经修改了我的答案。不太确定您要完成什么。如果您想在视图上使用这些属性,您只需在将视图模型及其任何属性提供给视图之前对其进行初始化。
    【解决方案2】:

    如果不将模型传递给视图,您将无法在视图中访问模型的属性。这就是错误的可能原因。

                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 
                  }
    

    【讨论】:

    • 我确实解决了这个问题,但它仍然卡住了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多