2011-08-12 6 views
1

Html.BeginForm에서 일부 HTML을 래핑하는 조건부 논리

@using(Html.BeginForm(MVC.Account.Info())) 
{ 
    <p>blah blah blah</p> 

    <input type="submit" value="Submit!" /> 
} 

이제는 폼을 렌더링하고 제출해야합니다. User.Identity.IsAuthenticated

들여 쓰기 레벨이 중요한 경우이

@if(User.Identity.IsAuthenticated) // wraps the using and its open brace 
{ 
    @using(Html.BeginForm(MVC.Account.Info())) 
    { 
} 

     <p>blah blah blah</p> 

@if(User.Identity.IsAuthenticated) // wraps the input and the using closing brace 
{ 
     <input type="submit" value="Submit!" /> 
    } 
} 

물론이 구문은 작동하지 않습니다. 누구든지이 일을하는 좋은 방법을 알고 있습니까?

답변

5

은 면도기를 사용하여 종료되었습니다. 난 당신이 좋은

@helper Info() 
{ 
    <p>blah blah blah</p> 
} 

@if(User.Identity.IsAuthenticated) 
{ 
    using(Html.BeginForm(MVC.Account.Info())) 
    { 
     @Info() 
     <input type="submit" value="Submit!" /> 
    } 
} 
else 
{ 
    @Info() 
} 
1
@if(User.Identity.IsAuthenticated) 
{ 
    @using(Html.BeginForm(MVC.Account.Info())) 
    { 
     <p>blah blah blah</p> 
     <input type="submit" value="Submit!" /> 
    } 
} 
else 
{ 
    <p>blah blah blah</p> 
} 

, 당신은 존재하는 경우 양식 내부의 <p>를 이동하는 자바 스크립트를 사용할 수 있습니다.

0

이있는 경우 여전히 멤버를 직접 처분하는 경우가 using을 제거 할 수있는 대안을 보는 상관 없어,이 최선의 방법이라고 생각합니다.

@{ 
    MvcForm htmlForm = null; 
    if (User.Identity.IsAuthenticated) 
    { 
     //render the begin of a <form> tag 
     htmlForm = Html.BeginForm(MVC.Account.Info()); 
    } 
} 

@* content goes here *@ 
<p>blah blah blah</p> 

@if (htmlForm != null) 
{ 
    //render the end of </form> 
    <input type="submit" value="Submit!" /> 
    htmlForm.EndForm(); 
} 
관련 문제