Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello below is my program that displays a list of questions based on the type of event id the user has selected. I am trying to figure out how can I hide the textbox that is for the AnswerText if the event id is 1 and then for any other event id the text box will be displayed.


@model IList<myProject.ViewModels.QuestionViewModel>

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml";

}

@{ 
    ViewBag.Title = "Event Questions";
}

<h2> Event Questions</h2>
@using (Html.BeginForm("PostAnswers", "EventQuestions", FormMethod.Post))
{
    <table class="table">
        @for (var i = 0; i < Model.Count(); i++)
        {

            <tr>
                <td>
                   
                 
                    @Html.HiddenFor(x => x[i].EventId)
                    @Html.HiddenFor(x => x[i].QuestionId)
                    @Html.DisplayFor(x => x[i].QuestionText)
                    <br />
                  
                    @if ()
                    {
                        @Html.HiddenFor(x => x[i].AnswerText, new { @class = "form-control" }) 
                    }
                    else
                    {
                        @Html.TextBoxFor(x => x[i].AnswerText, new { @class = "form-control" })
                    
                    }
   
                     

                  

                </td>
            </tr>
        }
    </table>

    <input type="submit" value="Submit" class="btn btn-primary btn-md" />
    @*Html.ActionLink("Submit", "PostAnswers")*@
   
}


What I have tried:

I was thinking along the lines of logic of if EventId = 1 {hides textbox} else {displays textbox}. Just don't know the correct syntax.
Posted
Updated 19-Mar-19 3:24am
v2

@if (Model[i].EventId == 1)
 
Share this answer
 
Comments
Member 13806552 19-Mar-19 9:28am    
Thank you this works.
C#
@if (eventID == 1)
{
    Html.HiddenFor(x => x[i].AnswerText, new { @class = "form-control" });
}
else
{
    Html.TextBoxFor(x => x[i].AnswerText, new { @class = "form-control" });
}
 
Share this answer
 
Comments
Member 13806552 19-Mar-19 9:25am    
That does not work unfortunately.
#realJSOP 19-Mar-19 10:02am    
Well, your question only provides half of the info necessary to answer it. You did not specify WHERE "eventID comes from,so I assumed it was a variable that you magically created. If it's in the page's model, or in another object, you of course have to modify what I told you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900