开发者

MVC3 razor syntax passing a parameter from a link to reset variable inside if statement

Forgive me if this seems like a simple task, I'm fairly new to this...

I'd like to create logic that allows the user to display or not display their email address when editing it from a dialog box. I am placing the link that will allow the user to 'opt out' inside the dialog box - and I'm trying to use the link to reset the variable inside the 'if' statement to 'false' The 'if' statement prevents the email address from being rendered.

Here is my if statement:

<div id="change-email" class="text"> 
    @{

        var showEmail = true;

        if (showEmail == true)
        {
            <text><p><span class="label">My email address: </span>@Model.E开发者_开发技巧mail</p></text>
        }
        else (showEmail == false)
        {
            <text><p>No email displayed</p></text>
        }

    }
</div><!--#change-email--> 

And here is the dialog box code:

<div id="dialog-email" class="modal">

    @using (Html.BeginForm("ChangeEmail", "Account", FormMethod.Post))
    {
        <fieldset>
           // form code here
        </fieldset>
    }

    <p><a href="" class="no-display">Do not display my email address.</a></p>

</div>

Any help would be appreciated...

Thanks!


If you do this with jQuery, and you were okay with the email address still being available in the page source, it would look like this:

<div id="change-email" class="text"> 
    <p><span class="label">My email address: </span>@Model.Email</p>
</div>
<div id="dialog-email" class="modal">

    @using (Html.BeginForm("ChangeEmail", "Account", FormMethod.Post))
    {
        <fieldset>
           // form code here
        </fieldset>
    }

    <p><a href="" class="no-display">Do not display my email address.</a></p>

</div>
<script type="text/javascript">
$(document).ready(function(){
    $('a.no-display').click(function(){
         $('#change-email p').text('No email displayed.');
    });
});
</script>

It would be a bit more involved if you wanted to persist the preference to not display email. You would probably want to add "Do not display my email address" as a check-box in the ChangeEmail form, adjust the Controller Action to which the form posts to handle the preference, and return it as a variable in the ViewBag of the View that the Action returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜