简体   繁体   中英

Use model on SharePoint application page .aspx

I am working on SharePoint to create a Feedback questionnaire form using an application page that is basically a aspx page.

I wish to do this by emulating MVC as far as possible. I've set up my model in the code-behind:

public List<QuestionViewModel> modelQuestions = new List<QuestionViewModel>();

Next, I need to display each question and an appropriate input depending on the question type (eg single line, multi line, single selection, multiple selection).

I've got it displaying the questions correctly:

<fieldset class="what-went-wrong">
    <% for (int i = 0; i < modelQuestions.Count; i++) { %>
        <p>
            <label for="QuestionText">
                <% if (modelQuestions[i].Required) { %>
                <span class="req-field indicator">*</span>
                <% } %>
                    <%= modelQuestions[i].QuestionText %>
                <% if (modelQuestions[i].Required) { %>
                <span class="req-field right">* Required field</span>
                <% } %>
            </label>
        </p>
    <% } %>
</fieldset>

This give's me the question text. I'm now trying to construct the appropriate input, but this <% %> tags is not working for this:

<% if(modelQuestions[i].QuestionTypeId == QuestionType.SingleLine) { %>
    <input id="modelQuestions_<% i %>" name="modelQuestions[<% i %>]" type="text" placeholder="<% modelQuestions[i].Placeholder %>" />
<% } %>

I can't seem to get it to construct the html element using details from the model (in the value for id, name, placeholder etc.)

Also, I've no idea how to go about posting this back to the server when I get to that point.

Is there any merit in continuing? Are there other controls/methods more appropriate to use in this case with aspx?

You cannot generate HTML markup like this. Even data-binding expressions will not help, because they bind ASP.NET controls' attributes values, not the plain output HTML in the page.

You should generate the markup in the "code behind" , like this:

Page markup:

<div id='AnswersPanel'>
<div>

Page code behind:

protected void PageLoad(...)
{
    AnswersPanel.InnerHtml = "";
    AnswersPanel.InnerHtml += string.Format("<input id='modelQuestions_{0}' name='modelQuestions[{0}]' type='text' placeholder='{1}' />",
                                                i.ToString(), 
                                              modelQuestions[i].Placeholder);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM