2013-02-07 4 views
0

내 ASP MVC보기에서 현재 날짜의 기본값을 제공하고 싶습니다. 그러나 View 코드에서이 작업을 수행하는 방법을 알 수 없습니다. 그러나 항상 최신 날짜가 아닐 수 있기 때문에이 필드를 업데이트 가능하도록 허용해야합니다. 어떤 제안?ASP MVC3에서 'editor-field'값을 설정하십시오.

<div class="M-editor-label"> 
     Effective Date 
    </div> 
    <div class="M-editor-field">    
     @Html.EditorFor(model => model.EffectiveDate) 
     @Html.ValidationMessageFor(model => model.EffectiveDate) 
    </div> 

편집

내가 좋아하는 모델에 기본 값이 필드를 제공하기 위해 노력했다 그래서

private DateTime? effectiveDate = DateTime.Now; 

    public Nullable<System.DateTime> EffectiveDate 
    { 
     get { return DateTime.Now; } 
     set { effectiveDate = value; } 
    } 

get 속성은 나에게 다음과 같은 오류 메시지가 있습니다 그러나 :

Monet.Models.AgentTransmission.EffectiveDate.get must declare a body because it is not marked abstract extern or partial

^(모네가 th입니다. 전자 프로젝트의 이름 및 AgentTransmission는 EFFECTIVEDATE이 속성으로있는 내가 있어요 현재 모델의 이름입니다.) 제안 당

SECOND 편집

답변 중 하나에 내가 설정 아래 그러나 생성자는 뷰가 렌더링 될 때 필드에 여전히 공백 값을 넣습니다.

public AgentTransmission() 
    { 
     EffectiveDate = DateTime.Now; 
    } 

THIRD을 편집 get로 위의 문제 수정

, 내가 지금까지 내 컨트롤러에 무슨 전체를 게시가.

public AgentTransmission() 
    { 
     EffectiveDate = DateTime.Today; 
     this.AgencyStat1 = new HashSet<AgencyStat>(); 
    } 

    //Have tried with an without this and got the same results 
    private DateTime? effectiveDate = DateTime.Today; 

    public Nullable<System.DateTime> EffectiveDate 
    { 
     get { return effectiveDate; } 
     set { effectiveDate = value; } 
    } 
+0

당신은 당신이 DateTime.Today로 model.EffectiveDate의 값을 기본 또는 오버라이드 (override) 할 건가요? – Peter

+0

오늘 기본값으로 설정하고 싶습니다. – NealR

+0

(질문 편집) – NealR

답변

0

나는, 이것을 해결 새 객체를 초기화하는 생성자. ... 이해하려고 노력

public ActionResult Create() 
    { 
     return View(new AgentTransmission()); 
    } 
0

모델 클래스의 생성자에서 기본값을 설정합니다. 이런 식으로 뭔가 : 다른 답변이 너무

public AgentTransmission() 
{ 
    EffectiveDate = DateTime.Today; 
    this.AgencyStat1 = new HashSet<AgencyStat>(); 
} 

private DateTime? effectiveDate; 

public Nullable<System.DateTime> EffectiveDate 
{ 
    get { return effectiveDate; } 
    set { effectiveDate = value; } 
} 

처럼 생성자에서 기본값을 만들고있는 특정 페이지에이 코드를 추가하여 제안으로

class YourClass { 
    public DateTime EffectiveDate {get;set;} 

    public YourClass() { 
    EffectiveDate = DateTime.Today; 
    } 
} 
+0

이렇게하면 효과가 있지만보기에는 아무 것도 표시되지 않는다고 생각했습니다. 나는 계단을 밟아'locals' 창을 확인했고, View가 렌더 될 때 여전히'null' 값을 가지고있는 것처럼 보입니다. – NealR

관련 문제